简体   繁体   中英

the main function in clang c99

In c99 standard, main function can be defined in two styles:

int main(void)

or

int main(int argc, char \* argv[])

But I tried (llvm 8 c99(-std=c99))

int main()/main()

and there is no warning or error.

How to understand the main definition in c99. and where to find the whole definition types of main function in clang?

There is int type is default for cases where it is omited. And for function return type too. The void type for funtion args is equal that function have no arguments. The empty args '()' mean that arguments and its count and its types is not specicfied.

Due to historical reasons, most compilers do not warn for int main() or just main() -- because that's how main() has been mostly before standardization of C.

GCC has some warning options which can detect it.

For 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]

and for 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]

There has been a bug report in llvm whichg seems to have fixed this very recently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM