1

I have wrote this basic C program:

int main(int argc, char const *argv[])
{
    int n = rand();
    int a[n];
    return a[0];
}

Which is getting compiled properly in gcc. But MS C/C++ intellisense in showing error squiggles stating expression must have a constant value C/C++(28) [4, 8]. After some googling I have found out that MSVC have not implemented VLAs. But I am unable to find a way to suppress this error. Someone please help me out!

EDIT: Also, surprisingly it is not warning for implicit declaration of rand in line 3 !!!

EDIT: If possible, can someone also mention an alternative extension for C/C++.

EDIT: I am using:

  • Microsoft VSCode v1.63.2 code editor
  • Microsoft C/C++ Extension Pack v1.1.10 extension
  • MinGW-W64 gcc v8.1.0 compiler

GCC no compilation flags are set.

C/C++ extension is configured with following:

  • compilerPath: C:\Program Files\mingw-w64\mingw64\bin\gcc.exe
  • cppStandard: gnu++20
  • cStandard: gnu17
  • intelliSenseMode: windows-gcc-x64
Sourav Kannantha B
  • 2,860
  • 1
  • 11
  • 35
  • Anyone? Please....! I am desperately trying to find it from 4 hours. :| – Sourav Kannantha B Dec 28 '21 at 17:55
  • Does this answer your question? [c++ array - expression must have a constant value](https://stackoverflow.com/questions/9219712/c-array-expression-must-have-a-constant-value) – Kamiccolo Dec 30 '21 at 12:31
  • @Kamiccolo Nope.. I am using C, and in C, standard allows VLA and gcc implements it. That question is about C++. – Sourav Kannantha B Dec 30 '21 at 12:34
  • You did not provide almost any information about your development environment. Build flags? Full toolchain with versions, etc? MVSC does not support it according to this: https://stackoverflow.com/a/5246961/1150918 godbolt fails as well. – Kamiccolo Dec 30 '21 at 12:39
  • This was fixed for clang: https://developercommunity.visualstudio.com/t/cc-intellisense-gives-parameter-is-not-allowed-for/1256096 – Andrew Henle Jan 05 '22 at 02:09

1 Answers1

-1

This shows as an error because it indeed is an error. When you're using the MSVC compiler it is only going to give you the results from MSVC compilation.

Now, this is actually due to the fact that you're doing the wrong thing. It seems that you want to initialize an array with a size n which is obtained from a random value. In other words, you should perform a dynamic allocation, that is, allocation of memory during runtime. Since you are using C, you must use the malloc functions for this as shown:

#include <stdlib.h>
int main(int argc, char const *argv[])
{
    int n = rand();
    int *a = malloc(n * sizeof(int));//Dynamic allocation of an array
    int val = a[0];//Garbage value
    free(a);//Deallocation / Free the memory taken by array 
    return val;
}

If want to replace MSVC with gcc, you can change the default compiler of C/C++ in VS code by changing the setting:

Change your setting

C_Cpp.default.compilerPath` : C:\MinGW\bin\g++.exe

EDIT: I mistakenly mentioned that the path should be of the bin folder. Instead, it should be of g++.exe itself. I downloaded the latest binary and confirmed it for you.

You can do this by going to the VS code preferences, searching for C_Cpp.default.compilerPath in the search bar, which would lead you to the required input box. Enter C:\mingw64\bin\gcc.exe and the compilation would replace MSVC with gcc/g++

Finally, try to avoid VLA and use dynamic allocation as it is a safer and much supported practice.

AzuxirenLeadGuy
  • 2,470
  • 1
  • 16
  • 27
  • `This shows as an error because it indeed is an error.` Yes, I know that is an error indeed, but I wanted to know how to suppress that in intellisense. – Sourav Kannantha B Dec 29 '21 at 06:45
  • Also, the code snippet shown in the question is just a minimal reproducible example for my case. I am using VLA in a different personal project. – Sourav Kannantha B Dec 29 '21 at 06:47
  • I have changed the settings to add `compilerPath`. But it is showing this: `[29/12/2021, 12:12:23 pm] Unable to resolve configuration with compilerPath "C:\MinGW\bin". Using "C:\MinGW\bin\gcc.exe" instead.` And the error still persists. – Sourav Kannantha B Dec 29 '21 at 06:48
  • @SouravKannanthaB You cannot suppress errors, You can only suppress warnings – AzuxirenLeadGuy Dec 29 '21 at 07:18
  • @SouravKannanthaB Can you show me the exact path you have entered? – AzuxirenLeadGuy Dec 29 '21 at 07:18
  • @SouravKannanthaB I have edited my answer to show another setting, I don't gave GCC installed in my machine so I cannot check on my own. Let me know if this works for you – AzuxirenLeadGuy Dec 29 '21 at 07:25
  • Exact path I have entered is `C:\MinGW\bin`. `where gcc` in terminal returns `C:\MinGW\bin\gcc.exe`. So I hope path I entered is correct. – Sourav Kannantha B Dec 29 '21 at 08:33
  • I have also changed the `intelliSenseMode`, but still the error persists. At this point, you can also suggest me a good alternative gcc extension for C/C++. I'm fed up with this. – Sourav Kannantha B Dec 29 '21 at 08:41
  • Check if this edit solves your problem – AzuxirenLeadGuy Dec 29 '21 at 09:59
  • Again, sorry, but nope, problem still persists. I've tried both `gcc.exe` and `g++.exe`. – Sourav Kannantha B Dec 29 '21 at 12:33
  • Also, I may reconsider my usage of VLAs. But I still want to get a solution for this question. – Sourav Kannantha B Dec 29 '21 at 12:35
  • @AzuxirenLeadGuy *This shows as an error because it indeed is an error* No it is not. VLAs were a mandatory part of C99 that Microsoft never bothered to support, and while they are now optional, the are supported by just about every major C compiler other than Microsoft's. In fact, this very bug in VSCode was fixed for clang recently: https://developercommunity.visualstudio.com/t/cc-intellisense-gives-parameter-is-not-allowed-for/1256096 Perhaps it's not been fixed for GCC, but I couldn't find a bug report (although my search was admittedly brief). – Andrew Henle Jan 05 '22 at 02:12
  • @AndrewHenle Yes it is not an error in GCC, (that's why it compiles successfully for the OP), but it is an error in the MSVC compiler and that is what I meant – AzuxirenLeadGuy Jan 05 '22 at 02:22