Why can i declare an array with a predestined size on gcc but not on visual studio c++?

后端 未结 2 1329
谎友^
谎友^ 2021-01-24 09:40

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];
<         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-24 10:40

    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:

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

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

    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_ _ The integer constant 1, intended to indicate that the implementation does not support variable length arrays or variably modified types.

提交回复
热议问题