gcc - Why does passing extra arguments to a C function does not result in a compile-time error? -


i able compile , run following code gcc 4.4.7.

file: m.c

#include <stdio.h>  int main() {     printf("%d\n", f(1, 2, 3)); } 

file: f.c

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

output:

$ gcc m.c f.c && ./a.out $ 3 

when function f() defined in same file, compiler throws error expected. guess compiler can not detect erroneous usage of functions between compilation units. should not linker able detect it? standard specify expected behavior?

please note different declaring function without parameters, works inside single file. (why gcc allow arguments passed function defined no arguments?).

i using gcc (gcc) 4.4.7 20120313 (red hat 4.4.7-11) , gnu ld version 2.20.51.0.2-5.42.el6 20100205.

gcc compiles -std=gnu89 default (not sure 5.x, it's true versions before). in c89 (gnu89 c89-superset), if function called without declaration being visible, assumed declared as

extern int f(); 

a function external linkage, returning int, , accepting unspecified (but fixed) number of default-promoted arguments.

this considered design mistake many, marked obsolescent in c89, , removed in c99. gcc gives warning implicit function declarations default.

if call function wrong type or number of arguments, behaviour undefined, diagnostic required if prototype-declaration in scope of call. standard imposes no requirements on implementation does, linker allowed fail (but doesn't).

use header files prototypes warning.

usually, c compiler compiles source files separately object files, symbols functions stored without information arguments' types, linker cannot check them.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -