我在 Xcode 中创建了一个基本的 C 项目,并稍微修改了 main.c 中的起始代码。我还进入了构建设置并告诉它使用 ANSI-C。这是我的代码:
int main(int argc, const char * argv[])
{
// a statement!
printf("Hello, World!\n");
// shouldn't this cause a compiler error?
// the variable isn't declared at the top of the scope.
int x;
x += 10;
return 0;
}
显然,它没有多大作用,但我预计变量声明会产生编译器错误(因为旧版本的 C 需要在作用域的开头,在其他语句之前声明变量)。然而,Xcode 愉快地编译并运行它,既没有错误也没有警告。
我可能在某个地方犯了一个愚蠢的错误,但我试图理解为什么这段代码可以编译。我读过 C99 和 C11 允许您在任何地方声明变量,所以这会起作用,但我明确地将项目设置为使用 ANSI-C。这只是 Apple 的 LLVM 编译器的工作方式吗?还是我在其他地方错过了什么?