【发布时间】:2012-11-30 00:33:43
【问题描述】:
我最近遇到了一个问题,以下玩具示例使用 clang -ansi 干净地编译:
int main(void)
{
for (int i = 0; 0; );
return i;
}
但gcc -ansi 给出以下错误:
a.c: In function ‘main’:
a.c:3:5: error: ‘for’ loop initial declarations are only allowed in C99 mode
a.c:3:5: note: use option -std=c99 or -std=gnu99 to compile your code
使用clang -ansi -pedantic 编译表明正在使用 C99 扩展。
a.c:3:10: warning: variable declaration in for loop is a C99-specific feature [-pedantic,-Wc99-extensions]
for (int i = 0; 0; );
^
1 warning generated.
clang 还允许使用-ansi 选项进行哪些其他扩展?如何禁用它们?
【问题讨论】:
-
几分钟前我遇到了一个类似的问题:clang 允许在同一块中的可执行代码之后定义变量 - 在 C99 中是合法的,但在 ANSI 中应该是非法的。
-
上面的完整示例在 C99 中也是非法的,因为在 return 语句中使用
i时没有定义它。 Clang 和 GCC 对此表示同意。似乎 C99 扩展的这种特殊用途使其合法。 -
你是对的,我没有在 OPs 代码中发现这一点(过于关注我自己的问题)。