compiling nested functions with clang versus gcc

最后都变了- 提交于 2019-12-02 12:40:16

问题


I have a c file that I can compile with no problem using GCC like below:

gcc foo.c

however using the same file I am receiving error of having defined functions inside main using clang:

clang foo.c

foo:230:1: error: function definition is not allowed here
{
^
foo.c:241:1: error: function definition is not allowed here
{
^
foo.c:253:1: error: function definition is not allowed here

these instances of errors are the definitions of a new function inside the main section of the code. I want to know why GCC doesn't get bothered with this yet clang does?


回答1:


Functions defined within functions are an extension to the C language, implemented by gcc. This is enabled by default. If you make gcc a Standard C compiler, as with -ansi -pedantic or -std=C99 or similar, it will also complain about nested function definitions:

x.c: In function ‘main’:
x.c:8:5: warning: ISO C forbids nested functions [-Wpedantic]
     int nested(void)
     ^


来源:https://stackoverflow.com/questions/43842531/compiling-nested-functions-with-clang-versus-gcc

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