Standard way to define parameter-less function main() in C

后端 未结 1 1665
北恋
北恋 2021-01-05 13:44

What is the correct way, according to the latest C standard, to define functions without parameters: int main() or int main(void)?

相关标签:
1条回答
  • 2021-01-05 14:22

    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).

    See the Standard (PDF)

    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.

    0 讨论(0)
提交回复
热议问题