Why am I being allowed to use a const qualified variable as an array size in C?

后端 未结 1 1814
北海茫月
北海茫月 2020-11-27 08:18

When I run the following code,it works fine for C:

#include

int main(void)
{

const int x=5;
char arr[x];
printf(\"%d\",sizeof(arr));

}


        
相关标签:
1条回答
  • 2020-11-27 09:02

    c99 support variable length arrays but c90 does not support variable length arrays, you can see this more clearly if you are using gcc and try to compile with these arguments:

    gcc -std=c89 -pedantic
    

    this will give you the following warning:

    warning: ISO C90 forbids variable length array ‘y’ [-Wvla]
    

    but if you compile using c99 it is perfectly fine:

    gcc -std=c99 -pedantic 
    

    As pointed out by John Bode as of the 2011 C standard variable length arrays(VLA) are now optional. Here is a Dr Dobbs article on VLA and also a link to the gcc docs as pointed out by Wayne Conrad.

    0 讨论(0)
提交回复
热议问题