0
int n=10;
int arr[n];

this code works fine in my GCC compiler. Isn't the size of static array is allocated at compilation time ? Shouldn't this code generate an error ?

Rohit Saxena
  • 61
  • 1
  • 5

1 Answers1

2

Variable length arrays are a C99 feature(optional in C11) and gcc supports this as an extension when not in c99 mode, one quick way to see this with gcc is to use the following:

gcc -std=c89 -pedantic

You will see the following warning:

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

but if you build using gcc -std=c99 -pedantic you will not see any warnings. From the C99 draft standard section 6.7.5.2 Array declarators paragraph 4:

[...] If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • For the sake of completeness. Visual C++ only supports C89 (which disallows dynamically-sized arrays), and C++ standards (none of which allows dynamically-sized arrays). So, if you use this feature, your code will be unsupported by Microsoft C compiler (it can still be compiled on Windows with MinGW). – Giulio Franco Aug 27 '13 at 20:24
  • 1
    @GiulioFranco Actually, until [recently](http://stackoverflow.com/a/17914975/1708801) VS did not support `C99`. – Shafik Yaghmour Aug 27 '13 at 20:30
  • @ShafikYaghmour this kind of progress is always nice, but unfortunately the article linked by the answer you linked says nothing about dyna-size arrays (it only speaks about library functions). I don't currently have VS2013 (has it even been released yet?) so I can't try. Besides, Microsoft documentation on C syntax still says "constant-expression". http://msdn.microsoft.com/en-us/library/5eay31s2.aspx – Giulio Franco Aug 27 '13 at 20:40