Take the 2-minute tour ×

Why does the following code compile with the Dev-C++ compiler and not with Visual Studio?

Any idea? Here is the code:

    int n,i;
    int arr[n];
         //Do something with the array 

Here are the errors:


share|improve this question
Mentioning the compile errors you get would help us to explain them. –  Roger Pate Nov 19 '09 at 20:48
Presumably your compile errors came with line numbers. Errors 3 and 4 are obvious, but I don't see where error 2 comes from, and there's the question of what error 1 is. –  David Thornley Nov 19 '09 at 21:50
Error 2 comes from the fact that in C89, all declarations have to be at the top of the function before any other statements. arr must be declared before the preceding scanf line. –  James McNellis Nov 19 '09 at 22:05
add comment

5 Answers

up vote 6 down vote accepted


int arr[n];

is invalid because n is not a constant expression. You need to allocate variable sized arrays on the heap using malloc (and then free them when you are done with free).

If you are trying to compile this with a .cpp extension, main must have a return type of int. If you are trying to compile this with a .c extension, then you need to use c-style local variable declaration and declare all of your local variables at the top of the function.

share|improve this answer
i just had a formating problem with SO editor :s –  Hannoun Yassir Nov 19 '09 at 20:42
The file has a .c extension –  Hannoun Yassir Nov 19 '09 at 20:46
I fixed the formatting (I think, I did it from my phone) –  Carson Myers Nov 19 '09 at 20:48
The arr[n] thing is likely your problem. GCC (which Dev-C++ uses) has preliminary support for C99 variable-length arrays, but Visual Studio's compiler does not, hence the compilation failure. –  bcat Nov 19 '09 at 20:48
add comment

Visual C++ doesn't do stack allocations with that syntax (though I wish it did). You can do stack allocations explicitly with:

int *arr = (int *)_alloca(n*sizeof(*arr));

and no need to free it since it's automatically freed when the scope ends.

share|improve this answer
add comment

This isn’t valid C++ – the Visual C++ compiler does not contain an up-to-date C compiler (rather a C subset of C++) and in particular it doesn’t implement C99 or anything newer. Your code uses features that the Visual C++ compiler doesn’t know (int arr[n]).

share|improve this answer
add comment

To simplify the answers you have gotten:

Your code is C99 and Visual C++ only supports C89. Do yourself a favour and get a better compiler for Windows. The Intel compiler has much better support for C99 than the Microsoft compiler (which has none).

share|improve this answer
add comment

Your program is not a Standard compliant program.

Any standard compliant compiler is required to issue a diagnostic when attempting to compile it.

If Dev-C++ compiled it without a warning, the compiler was invoked in a non compliance mode.

Other than the required diagnostic, a compliant compiler can attempt to compile anyway or just plainly abort compilation.


In C89 this is valid and requires no diagnostic, In C99 this is invalid and requires a diagnostic (valid C99 definitions are int main(void) or int main(int argc, char **argv) or equivalent) ... so if you are using a compliant compiler it is a C89 compiler.

int arr[n];

Oops, this is invalid in C89. In C89 you cannot have code intermixed with declarations. A C89 compiler must issue a diagnostic when it sees the declaration of the array.

So ... you are using your compiler in a non-conforming way. There's no way to tell why it compiles or fails to compile.

share|improve this answer
Moreover, in C89 it's invalid to declare an array with something that isn't an integer constant. In C++, it's legal to declare an array with a constant integral expression, but this isn't (the value of n occurs at runtime), and Visual Studio is likely going with the C++ standard here. –  David Thornley Nov 19 '09 at 21:49
Yeah ... and also fflush(stdin);, which is undefined in the Standard (*), and the missing return statement in main() (... and not checking the return value of scanf() before using the variable). (*) some compilers define flushing an input stream as an extension –  pmg Nov 19 '09 at 22:22
add comment

Your Answer


