Re:Deep C (2011)
by tom_mellior on Sunday October 22, 2017 @02:00PM
(#15528054)
int main() is not a prototype. int main(void) is a prototype. int main() declares main with an unknown (at this point) but fixed number of arguments (i.e., non-variadic). Callers must guess the correct number and types of arguments, the compiler does not enforce anything. In contrast, int main(void) declares main with exactly 0 arguments, to be enforced by the compiler.
For main it doesn't matter much since usually you don't call it yourself, but consider:
int f(); // no arguments, apparently
int g(int a, int b, int c) {
return f(a) + f(b, c); // at least one of these is fishy...
}
int f(double d) { // oh, the caller guessed wrong. twice.
return (d != 0.0 ? 0 : 1);
}
GCC and Clang do not complain about this program even with -Wall (they do with -Wstrict-prototypes).
Re:Deep C (2011)
by FeepingCreature on Sunday October 22, 2017 @01:03PM
(#15527733)
Apparently void foo() will not error when called with arbitrary values - it's implicitly variadic. This surprised me too, but I confirmed it with gcc, even on -std=c1x and with -Wall -Wextra -pedantic.
Learnt something!
Is there any good reason for writing "void" in empty parameter lists? I have never seen one and, unless there is one, that declaration is just useless line noise.