hi
can anyone explain the difference between
int main (void) vs int main ( )
in a simple way?
hi
can anyone explain the difference between
int main (void) vs int main ( )
in a simple way?
The C11 standard would disagree and what gcc does is immaterial.
For functions other than main(void) gcc on my system does produce different ASM for fn() and fn(void)
Edit:
Therefore int main() is implemenation-defined. Other functions are treated differently, but I can assure you that int fn() vs. int fn(void) generates different ASM using my gcc.1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent; or in some other implementation-defined manner.
E.g.
Is 100% compliant with the C11 standard and gcc produces different ASM for int fn(void) even if fn is called with no parameters. (If fn is int fn(void) then of course fn(78787) is an error, but calling it as fn() produces different ASM)Code:#include <stdio.h> int fn() { return 55; } int main(void) { printf("%d\n", fn(78787)); }
Last edited by Hodor; 04-04-2020 at 07:32 AM.
And as you quoted from C11, "5.1.2.2.1 Program startup"
It clearly shows main() defined with "void" as the parameter list, when no parameters are specified.
I stand by my statement, in a hosted environment:For main() and all other functions, I strongly recommend using full prototypes, and definitions.
From C11 draft standard:
Originally Posted by n1548
A little inaccuracy saves tons of explanation. - H.H. Munro
Yeah, I'm of the opinion that compilers that treat an empty identifier list in a function definition placed before another function definition as meaning "no information about the number or types of the parameters is supplied" when the definition is taken as serving as a forward declaration are likely non-conforming, but in theory this is academic because identifier lists are obsolescent, which leads me to my rant:
They didn't remove identifier lists in C17 even though there probably hasn't been new code written with non-empty identifier lists in over two decades, and my guess is that's because there's plenty of new code that uses empty identifer lists in function definitions or mistakenly in forward declarations to mean "no parameters". At some point they should admit that this is a mess that could be cleaned up by simply removing identifier lists and re-introducing empty identifier lists as a special case having semantics identical to void parameter type lists.
It'll upset those who have continued to use empty identifier lists in forward declarations to mean "no information about the number or types of the parameters is supplied", but that hasn't been good practice for awhile, and you get no sympathy for having the rug pulled from under you when you insist on using a feature that has been obsolescent for over two decades.
Last edited by laserlight; 04-04-2020 at 05:37 PM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)