What is the correct way, according to the latest C standard, to define functions without parameters: int main() or int main(void)?
- 698,132
- 130
- 858
- 1,229
- 29,421
- 31
- 118
- 202
1 Answers
Both forms of definition are valid (the one without void is an invalid prototype and an incomplete (albeit valid) declaration).
The form int main(void) { /* whetever */ } also provides a prototype for the function.
The form int main() { /* whatever */ } does not provide a prototype (and the compiler cannot check if it is called correctly).
6.7.5.3/14
An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters.
difference between definition: int main() { /* whatever */ }
and declaration: int main();
and prototype: int main(void);.
The definition does not provide a prototype;
the declaration is valid but specifies no information about the number or types of parameters;
the prototype is ok and compatible with the definition.
- 103,426
- 11
- 122
- 196