Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

warning: implicit declaration of function is invalid in C99?

Tags:

c

This is a header file

#include <stdio.h>

int m = 18;
int x = 4;

int singles (n) {
    if (n == 1)
         return 0;
    return doubles(n-1);
} 

int doubles (n) {
    if (n == 1)
        return 0;

    return triples(n-1);
}

int triples (n) {
    if (n == 1)
        return m;

    return (singles(n-1) + doubles (n-1) + triples (n-1))*(m-1);
}

and this is the main file

#include <stdio.h>
#include "test.h"

int main () {
    printf("%d",singles (x));
}

So this is pretty complicated for me at-least.The idea is that in the main function i will call singles(x) where x =4 so its more like singles (4),it will call doubles (3),that will call triples (2),that will call all of singles(1) which will return 0,doubles (1) that returns 0 and triples (1) that will return m.

So the error i am getting is

./test.h:13:12: warning: implicit declaration of function 'doubles' is invalid
      in C99 [-Wimplicit-function-declaration]
    return doubles(n-1);
       ^
./test.h:20:12: warning: implicit declaration of function 'triples' is invalid
      in C99 [-Wimplicit-function-declaration]
    return triples(n-1);
       ^
2 warnings generated. 

I tried to create a header file .h with the first script and then made a second .c script that i try to compile that won't work.I tried importing the header to try to avoid this error but it doesn't seem to work. Thanks a lot

like image 960
Charana Avatar asked Oct 13 '15 19:10

Charana


People also ask

What is an implicit declaration in error messages?

The term "implicit declaration" in an error message is usually generated when the compiler sees the implementation of a call to a function before the declaration (prototype). Also, in your function definitions (implementations) you need to specify the type of the argument:

What happens if a function is called without an explicit declaration?

In C90, if a function is called without an explicit declaration, the compiler is going to complain about the implicit declaration. Here is a small code that will give us an Implicit declaration of function error.

What is implicit declaration of function in C90?

In C90, if a function is called without an explicit declaration, the compiler is going to complain about the implicit declaration. Here is a small code that will give us an Implicit declaration of function error. Now the above code will give you an error of Implicit declaration.

Can I skip the declaration of a function in C99?

In the case of C99, you can skip the declaration but it will give us a small warning and it can be ignored but the definition of the function is important. This gives us the output:


2 Answers

Inside of singles, you're using doubles before it's defined. Similarly in doubles, you're using triples before it's defined. That's why you're getting the implicit declaration errors.

Also, you're not defining the type of the n parameter to any of these functions.

You need to specify function prototypes, which declare the function without defining it:

int singles(int n);
int doubles(int n);
int triples(int n);

Also, you shouldn't define functions in a header file. If you include this header in multiple .c files and then link them together, you'll get an error because you'll have multiple definitions of those functions.

Take all of the function definitions and put them in test.c. Then in test.h, put only the prototypes above. Then you can compile everything as follows:

gcc -c test.c
gcc -c main.c
gcc -o main main.o test.o

Or in a single line:

gcc -o main test.c main.c
like image 65
dbush Avatar answered Oct 17 '22 10:10

dbush


The term "implicit declaration" in an error message is usually generated when the compiler sees the implementation of a call to a function before the declaration (prototype).

For example, you should have:
header file

int singles(int x);
int doubles(int x);
int triples(int x);

In your source file:

#include "header_file.h"

Also, in your function definitions (implementations) you need to specify the type of the argument:

int singles (/* data type */ parameter_name)
{
 //...
}

Edit 1: Changed implementation to function call per @John Bollinger.

like image 4
Thomas Matthews Avatar answered Oct 17 '22 12:10

Thomas Matthews