-4

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?

Aaron
  • 75
  • 6

2 Answers2

5

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.

S.S. Anne
  • 14,415
  • 7
  • 35
  • 68
0

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

jacob13smith
  • 927
  • 4
  • 12
  • No reason to think the OP hasn't already done that. They say it's a C project. – Lightness Races in Orbit Nov 28 '19 at 23:55
  • Setting the Compile As setting forces the IDE to compile down to C. If OP is getting C++ errors/warnings, then they haven't done this. – jacob13smith Nov 28 '19 at 23:56
  • 1
    You are mistaken. There is no reason to think that VS is building their project as C++. _This is not a "C++ error"_. Visual Studio does not support VLAs either way. – Lightness Races in Orbit Nov 28 '19 at 23:58
  • OP says "the compiler tries to build it as if it was c++ code". This is reason to think that VS is building their project as C++. – jacob13smith Nov 29 '19 at 00:00
  • 1
    The OP is has presented absolutely no evidence for that claim and is, in all likelihood, wrong. And it doesn't matter: _even_ if the code was being compiled as C++, following your instructions _would not resolve the problem_ because Visual Studio still doesn't support VLAs in either language. – Lightness Races in Orbit Nov 29 '19 at 00:06
  • 1
    Yeah I was wrong, I only assumed that it was the problem. Thank you for the answer anyways! – Aaron Nov 29 '19 at 00:19