Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do these warnings mean C [duplicate]

Tags:

c

 final code2.c:9:1: warning: implicit declaration of function 'choice' is invalid in C99 [-Wimplicit-function-declaration]
choice();
^
final code2.c:12:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
choice()
^~~~~~
final code2.c:23:1: warning: implicit declaration of function 'wrong' is invalid in C99 [-     Wimplicit-function-declaration]
wrong();
^
final code2.c:25:1: warning: implicit declaration of function 'formula1' is invalid in C99 [-Wimplicit-function-declaration]
formula1();
^
final code2.c:27:1: warning: implicit declaration of function 'formula2' is invalid in C99 [-Wimplicit-function-declaration]
formula2();
^
final code2.c:29:1: warning: implicit declaration of function 'formula3' is invalid in C99 [-Wimplicit-function-declaration]
formula3();
^
final code2.c:30:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
final code2.c:32:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
wrong()
^~~~~
final code2.c:35:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
final code2.c:37:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
formula1()
^~~~~~~~
final code2.c:47:1: warning: implicit declaration of function 'question' is invalid in C99 [-Wimplicit-function-declaration]
question();
^
final code2.c:50:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
final code2.c:52:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
formula2()
^~~~~~~~
 final code2.c:63:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
final code2.c:65:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
formula3()
^~~~~~~~
final code2.c:85:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
final code2.c:87:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
question()
^~~~~~~~
final code2.c:99:1: warning: control reaches end of non-void function [-Wreturn-type]
 } 

I have these warnings that I have been trying to get rid of in my code. What do each of the warnings mean and whats the best way to get rid of them?

like image 772
John Smith Avatar asked Jun 27 '13 15:06

John Smith


4 Answers

They mean you're not declaring a return type for your functions, and that you're not using return statements after your compiler added a default int return type for those functions. Declare them all void and the warnings will disappear.

EDIT: And declare your functions before you use them, also, either in a header file, or just above the code where they're called.

like image 118
Crowman Avatar answered Nov 17 '22 16:11

Crowman


You have a function prototype that looks like this:

choice();

In C89, that was fine, and it would implicitly become:

int choice();

In C99, you need to explicitly add the return type. It also looks like you're intending it to be void, so you'll want:

void choice();
like image 39
Bill Lynch Avatar answered Nov 17 '22 17:11

Bill Lynch


Looks like you have a function choice() that you did not give a return type. If you intend for it to be void declare as:

void choice();
like image 31
eliteslayer Avatar answered Nov 17 '22 17:11

eliteslayer


You're not declaring your functions before you use them. C requires that you declare functions before you use them. Typically you put the declarations in a header file.

bad-example.c:

void myFunc() {
   // This causes an implicit declaration; myTest() has not yet been defined.
   myTest(); 
}

void myTest() {
}

good-example.c:

void myFunc();
void myTest();

void myFunc() {
   myTest(); 
}

void myTest() {
}
like image 22
antiduh Avatar answered Nov 17 '22 17:11

antiduh