1

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

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

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Does it have anything to do with c89 or c99 standard?

YES!
MSVC doesn't support C99. Variable length arrays are C99 feature.

GCC also allow VLA as an extension so, you can compile your code in C90 mode.

6.19 Arrays of Variable Length

Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++.

2 Comments

Which version of GCC are you using? The latest is 4.8.2 and you code is compiling with warning C89 forbids variable length array 'vec' [-Wvla]. This may be a compiler extension.
@Vcoder You need to use the -pedantic to obtain a warning since gcc support VLA as an extension.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.