c - 语句后的声明,gcc 4.5 vs visual c 2005

标签 c visual-studio visual-c++ gcc

以下代码可以在 gcc 4.5 中编译,但不能在 visual c 2005 中编译。

int main()
{
    int len;
    len = 32;
    char buff[len];

    return 0;
}

我知道我在语句之后声明数组,这违反了 ANSI C 规则。

但是为什么 GCC 没有给出任何错误或警告,而在 visual c 中,它给出了这样的错误,

error C2143: syntax error : missing ';' before 'type'

有什么想法吗?

谢谢。

最佳答案

您的 C 代码不符合 C90。 gcc -pedantic 将对此发出警告。

~/tmp$ gcc -pedantic a.c
a.c: In function ‘main’:
a.c:5: warning: ISO C90 forbids variable length array ‘buff’
a.c:5: warning: ISO C90 forbids mixed declarations and code
~/tmp$ 

关于c - 语句后的声明,gcc 4.5 vs visual c 2005,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6356059/

相关文章:

c - 如何使用fork()创建这个进程树?

c++ - 如何在Visual Studio/OpenGL中设置GPU

c++ - 提高 Windows 应用程序的性能

c - 使用microsoft Visual C++ 2008调试C程序

c++ - 在 Boost zip 迭代器上使用 C++17 并行执行算法时,为什么会出现 MSVC 错误?

C 编程 - printf 语句给出 "expression expected"错误

用 C 计算算法运行时间

c - 如何在 C99 中用另一个结构成员初始化 void* 数据结构成员?

c++ - 如何打印出 Visual Studio 跟踪点中的寄存器偏移量?

c++ - Unresolved external symbol 错误 2001