问题
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