Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is GCC inconsistent about emitting warnings for undeclared functions?

Tags:

c

gcc

warnings

The following test.c program

int main() {
   dummySum(1, 2);
   return 0;
}

int dummySum(int a, int b) {
   return a + b;
}

...doesn't generate any warning when compiled with gcc -o test test.c, whereas the following one does:

int main() {
   dummySum(1, 2);
   return 0;
}

void dummySum(int a, int b) {
   a + b;
}

Why?

like image 734
Manuel Selva Avatar asked Feb 23 '12 15:02

Manuel Selva


1 Answers

When faced with an undeclared function, the compiler assumes a function that accepts the given number of arguments (I think) and returns int (that part I'm sure of). Your second one doesn't, and so you get the redefinition warning.

I believe, based on a very quick scan of the foreward, that C99 (PDF link) removed this. No great surprise that GCC still allows them (with a warning), though; I can't imagine how much code would start failing to compile...


Recommend using -Wall (turning on all warnings) so you get a huge amount of additional information (you can turn off specific warnings when you have a really good reason for whatever you're doing that generates them if need be).

like image 148
T.J. Crowder Avatar answered Sep 18 '22 23:09

T.J. Crowder