What does the c compiler do when it does not find the corresponding function

Consider the following code:

#include<stdio.h> int f() { printf(" hey "); return 5; } int main() { printf("hello there %d",f(4,5)); f(4,5); return 0; } 

I expected something like too many arguments for the function 'int f (), but it gives the result even in strict compilation of C99. why is this behavior? But it looks like the C ++ compiler is giving an error.

+4
source share
2 answers

C is much less strict than C ++ in some respects.

The signature of the f() function in C ++ means a function with no arguments, and the appropriate compiler must provide this. In C, on the contrary, the behavior is not defined, and compilers do not need to diagnose if a function is called with arguments, even if it was defined without parameters. In practice, modern compilers should at least warn about inappropriate use.

Also, in function prototype declarations (without definition) in C, empty brackets mean that parameters are undefined. Subsequently, it can be defined with any number of arguments.

To prevent this, use the following ISO / IEC 9899 6.7.6.3/10 prototype :

 int f(void) 
+8
source

In addition to what Conrad wrote, C will also implicitly declare functions to you. For instance:

 int main(int argc, char** argv) { int* p = malloc(sizeof(int)); return 0; } 

will compile (possibly with a warning) without #include <stdlib.h> , which contains the declaration.

+2
source

All Articles