Let's at first consider how the function main
shall be declared according to the C17 Standard. The C17 Standard, section 6.7.6.3 Function declarators (including prototypes):
14 An identifier list declares only the identifiers of the
parameters of the function. An empty list in a function declarator
that is part of a definition of that function specifies that the
function has no parameters. 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.
That is empty parentheses can be used in a function definition when the function has an empty identifier list. But relative to the function main
the Standard requires that it will be declared with a parameter type list.
From the C grammar:
5 If, in the declaration "T D1", D1 has the form
D ( parameter-type-list )
or
D ( identifier-listopt )
and the type specified for ident in the declaration "T D" is
"derived-declarator-type-list T", then the type specified for ident is
"derived-declarator-type-list function returning the unqualified
version of T"
As it is seen the parameter type list may not be optional. It is the identifier list that may be optional.
Further (p. #10):
10 The special case of an unnamed parameter of type void as the only
item in the list specifies that the function has no parameters.
That is the function main
without parameters shall be declared with a parameter list that contains unnamed parameter of type void
.
From the C17 Standard (5.1.2.2.1 Program startup):
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;10) or in some other implementation-defined manner.
Now let's read the footnote 10 that elaborates what means the word equivalent:
10)Thus, int can be replaced by a typedef name defined as int, or the
type of argv can be written as char ** argv, and so on.
There is nothing said that the function declaration of the function main
with an empty parameter list may be rewritten with a function declaration with an empty identifier list.
That is the Standard requires that the function main
shall be declared with the function prototype. And examples of such declarations of main
are presented in the quote provided above.
From the C17 Standard (6.9.1 Function definitions)
Semantics
7 The declarator in a function definition specifies the name of the
function being defined and the identifiers of its parameters. If the
declarator includes a parameter type list, the list also specifies the
types of all the parameters; such a declarator also serves as a
function prototype for later calls to the same function in the same
translation unit. If the declarator includes an identifier list,166)
the types of the parameters shall be declared in a following
declaration list. In either case, the type of each parameter is
adjusted as described in 6.7.6.3 for a parameter type list; the
resulting type shall be a complete object type.
Pay attention to that the verb shall in the C standard means the following
- Conformance
1 In this document, "shall" is to be interpreted as a requirement on
an implementation or on a program; conversely, "shall not" is to be
interpreted as a prohibition.
What is changed in the C23 Standard relative to the declaration of main
? Nothing! If you will read the section 5.1.2.2.1 Program startup of the C23 Standard Draft you will see that there are used the same text relative to the declaration of main
without parameters.
Yes, now in the C23 Standard a function declaration with empty parentheses in the function declarator has another meaning. The parameter type list can be optional. The identifier list is excluded from function declarators. But the declaration of main
satisfies the same requirements as in the C17 Standard. It shall be declared as written in the C23 Standard like
int main( void )
and even the footnote stays the same as in the C17 Standard. There is no a word that the function main
may be declared like
int main()
in the section 5.1.2.2.1 Program startup of the C23 Standard.
The form of the declaration of main
with empty list of parameters was preserved in the C23 Standard in the same way as in the C17 Standard.
And @zwol
mistakenly claims relative to the declaration of main
that "All code fragments in the standard are considered to be examples, and, therefore, non-normative." The section 5.1.2.2.1 Program startup of the both Standards, the C17 Standard and the C23 Standard, has normative description of how the function main shall be declared.
You should pay attention to that it will be a serious change in the normative description of main
from preceding C Standards and the C23 Standard. And the C23 Standard should reflect that in its description "what documents, for all intents and purposes, have been applied to this draft (C23)". However in this list of documents nothing is said about changes in the requirements for declaration of the function main
. I found only the following document that has some relation "N2432 Remove support for function definitions with identifier lists".
If to adopt that I am mistaken then as a consequence of that it may be concluded that the C23 Standard has a defect because this section was transferred from the C17 Standard without any changes but its meaning was changed. At least in the C23 Standard there should be added a note or a footnote in this section that clarifies changes relative to the previous C Standard.
main
asint main(int argc, char **argv, char **envp)
, in which caseenvp
is aNULL
-terminated array of environment variable definitions. I just tried this on my Linux system, and it still works. Not sure what ANSI C has to say about it. It's probably considered non-standard.main
asint main() { /*code here*/ }
has been equivalent toint main(void) { /*code here*/ }
at least as far back as C11.-std=c11
. godbolt.org/z/5zf1dr1dK shows GCC acceptingmain(123)
after anint main(){}
definition with no warnings even at-Wall -Wextra -pedantic
. Clang warns about deprecation but doesn't error, with the message saying it's only an error in C2x. GCC-std=c23
does error. It that item in the standard just talking about there being no named vars you can access to read any parameters that got passed? Otherwise GCC and Clang would be deviating from the C11 standard, perhaps intentionally, but then what's C23 changing?int main() {/*...*/}
is a valid definition formain
" Emphasis on definition added. Notice also the emphasis on definition in my comment. Your comment is about whether a definition with an empty argument list acts as a prototype. The answer to that question in C11 is "No". In C23, the answer is "Yes".