在c99标准中,main函数可以定义为两种样式:
int main(void)或
int main(int argc, char \* argv[])但我试过了(llvm 8 c99(-std=c99))
int main()/main()并且没有警告或错误。
如何理解c99中的main定义。在clang中,哪里可以找到main函数的完整定义类型?
发布于 2017-01-21 17:16:14
对于省略它的情况,默认情况下是int类型。对于函数返回类型也是如此。函数参数的void类型等于没有参数的函数。空参数'()‘表示未指定参数及其计数和类型。
发布于 2017-01-23 18:46:13
由于历史原因,大多数编译器不会警告int main()或只警告main() --因为在C语言标准化之前,main()大部分都是这样的。
GCC有一些可以检测到它的警告选项。
对于main()
$ gcc -Wall -Wextra -Wold-style-declaration -Wold-style-definition -Wstrict-prototypes -std=c99 test.c
test.c:4:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
main()
^~~~
test.c:4:1: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
test.c: In function ‘main’:
test.c:4:1: warning: old-style function definition [-Wold-style-definition]而对于int main()
$ gcc -Wall -Wextra -Wold-style-declaration -Wold-style-definition -Wstrict-prototypes -std=c99 test.c
test.c:4:5: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
int main()
^~~~
test.c: In function ‘main’:
test.c:4:5: warning: old-style function definition [-Wold-style-definition]在llvm中有一个bug report,它最近似乎修复了这个问题。
https://stackoverflow.com/questions/41777574
复制相似问题