简体   繁体   中英

Why is GCC inconsistent about emitting warnings for undeclared functions?

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?

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).

A function cannot be used before it has been declared. When a function declaration is not visible, the implementation assumes in C89 that the function:

  • takes an unspecified (but fixed) number of parameters

  • returns an int

This is called an implicit function declaration .

In C99, implicit declarations of function have been removed of the language and the implementation is free to refuse to translate the source code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM