Trying to write C code in VS, cannot build due to C++ errors [duplicate]

谁都会走 提交于 2020-01-07 09:54:23

问题


I'm trying to use the latest Visual Studio 2019 Community to run some C projects, but the compiler tries to build it as if it was c++ code. For example, declaring an array like this throws the error: "expression must have a constant value"

#include <stdio.h>
int main(){
int input;
scanf("%d", &input);
int array[input];
return 0;
}

I understand that C++ doesn't allow declaration like this, but my source file is in .c format so why doesn't it just build and run like in every other IDE?


回答1:


This isn't a C++ error; VLAs (variable-length arrays) aren't supported in Visual Studio. You'll have to use a constant array size.




回答2:


You can create a C-language project by using C++ project templates. In the generated project, locate files that have a .cpp file name extension and change it to .c. Then, on the Project Properties page for the project (not for the solution), expand Configuration Properties, C/C++ and select Advanced. Change the Compile As setting to Compile as C Code (/TC).




回答3:


It doesn't matter whether it's C or C++, your code won't compile anyway

int array[input];

You can't have an variable as the size of the array. That's what your compiler complains about.



来源:https://stackoverflow.com/questions/59097259/trying-to-write-c-code-in-vs-cannot-build-due-to-c-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!