问题描述
我在
main() return;
或
main() if();
并获得了不同的错误,最特别的是
and obtained different errors, the most peculiar of which was
/usr/lib/gcc/i686-linux-gnu/4.4.5/../../../../lib/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
虽然一个程序只需要一个语句是不常见的,为什么main()要求有大括号?
While it's uncommon for a program to require only one statement, why does main() make it a requirement to have braces?
有人可以解释为什么错误所以特别的时候编译只是int main();?
Could someone explain why the error was so peculiar when compiling just int main();?
推荐答案
因为你正在定义一个名为 main()
函数定义基本上是一个函数声明( int main()
part)后跟一个复合语句( {/ * ... * /}
part)(你也可以使用一个函数try块,但是这些很少使用,并且仍然需要大括号)。
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.
这篇关于为什么main()需要花括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!