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

发布于 2024-08-12 08:29:27 字数 561 浏览 4 评论 0原文

为什么以下代码使用 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();
}

以下是错误:

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

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 http://img688.imageshack.us/img688/6618/26863513.jpg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

水晶透心 2024-08-19 08:29:27

int arr[n];

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

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

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.

狠疯拽 2024-08-19 08:29:27

Visual C++ 不使用该语法进行堆栈分配(尽管我希望这样做)。您可以使用以下命令显式进行堆栈分配:

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

并且无需释放它,因为它会在作用域结束时自动释放。

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.

随心而道 2024-08-19 08:29:27

这不是有效的 C++ – Visual C++ 编译器包含最新的 C 编译器(而不是 C++ 的 C 子集),特别是它不实现 C99 或更新的版本。您的代码使用了 Visual C++ 编译器不知道的功能 (int arr[n])。

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

暖阳 2024-08-19 08:29:27

为了简化您得到的答案:

您的代码是 C99,而 Visual C++ 仅支持 C89。帮自己一个忙,获得一个更好的 Windows 编译器。 Intel 编译器对 C99 的支持比 Microsoft 编译器(Microsoft 编译器没有)好得多。

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

迷荒 2024-08-19 08:29:27

您的程序不是符合标准的程序。

任何符合标准的编译器在尝试编译时都需要发出诊断。

如果 Dev-C++ 在没有警告的情况下编译它,则编译器将以非合规模式调用。

除了所需的诊断之外,兼容的编译器还可以尝试编译或直接中止编译。

main()

在 C89 中,这是有效的,不需要诊断,在 C99 中,这是无效的,需要诊断(有效的 C99 定义是 int main(void)int main(int argc, char **argv ) 或同等版本)...所以如果您使用兼容的编译器,它就是 C89 编译器。

scanf("%d",&n);
int arr[n];

哎呀,这在 C89 中无效。在 C89 中,不能将代码与声明混合在一起。 C89 编译器在看到数组的声明时必须发出诊断。

所以...您正在以不符合标准的方式使用编译器。没有办法告诉它为什么编译或编译失败。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文