I know that “the empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied„[1]:
// No information about the parameters is supplied. int foo();I know that “an empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters„[2].
// In this case the foo() function has no parameters. int foo() { // ... }I know that “the special case of an unnamed parameter of type
voidas the only item in the list specifies that the function has no parameters„[3]:// foo() has no parameters. int foo(void); // bar() has no parameters. int bar(void) { // ... };
So here are some questions:
Is
int main() { /* ... */ }legal? The standard states[4] thatThe function called at program startup is named
main. The implementation declares no prototype for this function. It shall be defined with a return type ofintand with no parameters:int main(void) { /* ... */ }or with two parameters (referred to here as
argcandargv, 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.
So, is
int main() { /* ... */ }is equivalent toint main(void) { /*... */ }?Why GCC allows pass parameters to a function that has no parameters?
int foo(); int bar() { return 42; } int main(void) { // Should be OK. foo(13); // Should give a compile-time error. bar(1, 12); }But I actually can compile the program with
gcc version 10.1.0 (GCC):gcc -std=c17 -Werror -c test.c.
I've read some related questions, such as What are the valid signatures for C's main() function?, but they do not take into account the following standard clause[2]:
An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters.
So, is my understanding of this clause correct?
- ISO/IEC 9899:2017 § 6.7.6.3 / 14.
- ISO/IEC 9899:2017 § 6.7.6.3 / 14.
- ISO/IEC 9899:2017 § 6.7.6.3 / 10.
- ISO/IEC 9899:2017 § 5.1.2.2.1 / 1.