为什么我可以在gcc上声明一个具有预定大小的数组,但不在Visual Studio c ++上?

为什么我可以在gcc上声明一个具有预定大小的数组,但不在Visual Studio c ++上?

问题描述:

我想知道为什么我可以在linux下做这个代码,但不是在Visual Studio上?
(File - > main.c)

i was wondering why i can do this code in linux but not on visual studio? (File -> main.c)

int size;
printf("Size:");
scanf("%d",&size);
int vec[size];

它与c89或c99标准有什么关系?因为我试图改变gcc上的标志,并仍然解决。

Does it have anything to do with c89 or c99 standard? Cause i tried to change the flags on gcc and still worked out.

变长数组 VLA )是C99的标准添加,直到最近 Visual Studio没有支持C99,据我所知不支持VLA。如果在C99模式之外的gcc中构建它,我们可以在 c90 模式下使用它将提供的 -pedantic 标志警告:

Variable length arrays(VLA) is a C99 addition to the standard and until recently Visual Studio did not support C99 and as far as I know does not support VLA. If you build this in gcc outside of C99 mode let's say in c90 mode and use the -pedantic flag it will provide a warning:


警告:ISO C90禁止变量数组'vec'[-Wvla]

warning: ISO C90 forbids variable length array 'vec' [-Wvla]

gcc 将支持,甚至在C ++中。

gcc will support VLA as an extension outside of C99 mode and even in C++.

请注意, C11 使VLA成为可选项,我们可以从草稿C11标准部分 6.10.8.3 条件特征宏中看到,其中包含以下项目符号: p>

Note that C11 made VLA optional, we can see that from the draft C11 standard section 6.10.8.3 Conditional feature macros which includes the following bullet:


_ _STDC_NO_VLA_ _ 整数常量 1 ,用于表示
实现不支持变长数组或可变
修改类型。

_ _STDC_NO_VLA_ _ The integer constant 1, intended to indicate that the implementation does not support variable length arrays or variably modified types.