为什么使用Dev-C++编译器而不是Visual Studio编译器?

Han*_*sir 0 c dev-c++ visual-studio

为什么以下代码使用Dev-C++编译器而不是Visual Studio编译?

任何的想法?这是代码:

#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();
}
Run Code Online (Sandbox Code Playgroud)

以下是错误:

错误http://img688.imageshack.us/img688/6618/26863513.jpg

Jam*_*lis 6

这个:

int arr[n];
Run Code Online (Sandbox Code Playgroud)

无效,因为n它不是常量表达式.您需要在堆上分配可变大小的数组malloc(然后在完成后释放它们free).

如果您尝试使用.cpp扩展名编译它,则main必须具有返回类型int.如果您尝试使用.c扩展编译它,则需要使用c样式的局部变量声明并在函数顶部声明所有局部变量.

  • `arr [n]`可能是你的问题.GCC(Dev-C++使用)初步支持C99可变长度数组,但Visual Studio的编译器没有,因此编译失败. (5认同)