Why does main() require braces?

孤街醉人 提交于 2019-12-06 04:19:39

Because you are defining a function named main() and a function definition is basically a function declaration (the int main() part) followed by a compound statement (the { /* ... */ } part) (you could also use a function try block, but those are very rarely used and still require braces).

You can't define any function without braces.

It's not unique to main -- the body of any function must be surrounded by braces. Specifically §8.4/1 defines a function-body as a "compound-statement" (and, for the truly pedantic, §6.3/1 defines a compound-statement as: "{ statement-seqopt }".

Because it is a function. It's part of the syntax.

Because the C++ standard says that all functions with a body must have braces. That's just the way the standard is defined, for better or for worse.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!