开发者

c - declaration after statment , gcc 4.5 vs visual c 2005

开发者 https://www.devze.com 2023-03-12 22:29 出处:网络
Following code does compile in gcc 4.5 but it is not being compiled in visual c 2005. int main() { int len;

Following code does compile in gcc 4.5 but it is not being compiled in visual c 2005.

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

    return 0;
}

I kn开发者_如何学编程ow that i am declaring array after statment, which is against ANSI C rules.

But why GCC does not give any error or warning, and in visual c, it gives error like,

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

any ideas?

Thanks.


Your C code is not C90 compliant. gcc -pedantic will warn about this.

~/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$ 


Visual Studio 2005 compiler cannot handle dynamic array allocation. Try char buff[32], it will work. It is a limitation of VS 2005.

0

精彩评论

暂无评论...
验证码 换一张
取 消