1

I wrote a simple prototype code just to show the problem:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>


int main(int argc, char const *argv[])
{
    int rows, columns;
    int arr[rows][columns];

    printf("%i", arr[0][0]);
}

Compiling this code I get no errors/warnings but in my IDE (visual studio code), the variables rows and columns are underlined in red and upon hovering over them, I get the error/message saying expression must have a constant value.

Is it a problem in the code or just the IDE? The code compiles and runs without any problems and runs exactly as I need it to. Any idea why this is?

Many thanks.

Slash
  • 55
  • 1
  • 8
  • The most vivid problem is that your variables rows and columns are uninitialized. They don't have a value. The error text seems to also indicate that not only do they need to have values, but those values must be constant values. That would be the case in C++. I am a little surprised as I was under the impression that C supports variable length arrays – Armen Tsirunyan May 15 '21 at 13:48
  • @ArmenTsirunyan even when values are assigned to them the same error is shown. From the research I've done, C does support variable-length arrays and this is how you initalize them (granted `int rows, columns;` should have values assigned to them, eg, `int rows = 1, columns = 2;`. Seems to be a problem with the IDE but I just wanted to have it confirmed. – Slash May 15 '21 at 13:51
  • I believe your IDE is set to C++ mode, which does not support VLAs. If you can set it to recognize C99 syntax, it should work. – abelenky May 15 '21 at 13:52
  • 1
    @abelenky I'm using the c/c++ extension in VS code. From my understanding, I dont think it's possible to use just the C part of the extension. But, from your suggestion, I tried using a different IDE and got no problems. So seems like you were right. Thanks. – Slash May 15 '21 at 13:56
  • @Slash MSVC doesn't support C99. – Andrew Henle May 15 '21 at 14:30
  • @AndrewHenle can you please tell how to suppress that error? – Sourav Kannantha B Dec 28 '21 at 13:55
  • @SouravKannanthaB Use another compiler. MSVC will never support VLAs. – Andrew Henle Jan 04 '22 at 22:06
  • @Andrew I am actually using `minGW-GCC` compiler. If you wish, visit [this](https://stackoverflow.com/q/70508254/12357035) question I asked. – Sourav Kannantha B Jan 04 '22 at 23:10

1 Answers1

2

C supports variable length arrays since C99, so your array declaration is valid. Your IDE might be set to C++ mode or to a C standard before C99 and therefore throw that warning.

On a side note, you did not initialize your rows and columns variables or the array content, meaning their values are undefined.

lulle2007200
  • 888
  • 9
  • 20
  • 1
    [MSVC doesn't support C99](https://stackoverflow.com/questions/48615184/does-visual-studio-2017-fully-support-c99). Hey, it's only been 22 years. Give the folks at Microsoft some time. You can't expect them to support last century's C standard when only one fifth of this century has gone by... – Andrew Henle May 15 '21 at 14:13
  • See also [**Is Visual Studio 2019 Compatible with C99?**](https://stackoverflow.com/questions/48615184/does-visual-studio-2017-fully-support-c99) – Andrew Henle May 15 '21 at 14:14
  • Huh, wasn't aware. But then again, i've been using clang toolchain and clangd since forever now... – lulle2007200 May 15 '21 at 14:15
  • Yes, something along the lines of, "What?!?! MSVC doesn't fully support C ***99***?!?!" is usual. [Their low standards for supporting C are fully evident here](https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-support-arriving-in-msvc/): "Our team is happy to announce that C11 and C17 are becoming supported language versions in the MSVC compiler toolset starting with Visual Studio 2019 version 16.8 Preview 3!" Ouch. I'm not sure I'd be bragging about being that late to the party. Especially given they skipped C99. They're not even **supported**. They're **BECOMING supported**. – Andrew Henle May 15 '21 at 14:17