views:

339

answers:

5

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

Any idea? Here is the code:

#include<stdio.h>
main(){
    int n,i;
    scanf("%d",&n);
    int arr[n];
    for(i= 0 ; i <n ; i++)
    {
         //Do something with the array 
    }
    fflush(stdin);
    getchar();
}

Here are the errors:

Errors

+6  A: 

This:

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.

James McNellis
i just had a formating problem with SO editor :s
Yassir
The file has a .c extension
Yassir
I fixed the formatting (I think, I did it from my phone)
Carson Myers
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
+3  A: 

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]).

Konrad Rudolph
A: 

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.

main()

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.

scanf("%d",&n);
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.

pmg
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
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
+4  A: 

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.

Jim Buck
+1  A: 

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).

Simon