What is the correct way, according to the latest C standard, to define functions without parameters: int main() or int main(void)?
- 730,956
- 141
- 904
- 1,278
- 30,618
- 31
- 128
- 208
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.
- 106,608
- 13
- 126
- 198