C Function with parameter without type indicator still works?

This behaviour is to provide backwards compatibility with older versions of the language, the K&R version of the language. When GCC encounters an “old style” function, it conforms to the old K&R C behaviour which implies no warnings in this situation.

Indeed, if you change the function to: int func(int param111), you do get the expected warnings:

x.c: In function ‘main’:
x.c:11:5: error: too many arguments to function ‘func’
x.c:2:5: note: declared here
x.c:12:5: error: too many arguments to function ‘func’
x.c:2:5: note: declared here
x.c:14:1: warning: control reaches end of non-void function [-Wreturn-type]

(Tested with GCC 4.7.3 and “gcc -std=c99 -Wall x.c && ./a.out”)

Or to quote JeremyP from the comments: “In K&R C it was perfectly fine to call a function with as many arguments as you like, because the ellipsis notation wasn’t invented then.”.

Note that a compiler can show as many extra warnings it wants and still conform to the standard. For instance Apple’s compiler warns about this code.

Leave a Comment