2

What I'm trying to do:
I need to write integers into a 2d array. The length of the array is N*N. So I scanf to get the value of N from the user.

The C/C++ extension gives the "expression must have a constant value". But building it with gcc works perfectly fine

What I've tried:
C/C++ extension gives error on N in both arrays, "expression must have a constant value".

After some googling answers, I tried to set my compiler version in the extension to c99, since that is the version which supports variable length arrays. But it still give the same error. Tried using other newer c versions, the intellicense still gives the same error.

Code and settings:
tree.c:

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

int main() {
    int N, i, j;
    scanf("%d", &N);
    int min_tree[N][N];
    int tree_walked[N];
}

c_cpp_properties.json:

{
    "name": "TUF_Laptop",
    "includePath": [
        "${workspaceFolder}/**",
        "/usr/local/include",
        "/usr/include",
        "/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include",
        "/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include-fixed",
        "/usr/lib/gcc/x86_64-pc-linux-gnu"
    ],
    "defines": [
        "_DEBUG",
        "UNICODE",
        "_UNICODE"
    ],
    "compilerPath": "/usr/bin/gcc",
    "intelliSenseMode": "linux-gcc-x64",
    "compilerArgs": [
        "-O3 -Wall -Wextra -std=c99"
    ],
    "cStandard": "c99",
    "cppStandard": "gnu++17",
    "compileCommands": ""
}

Similar answers I found: "expression must have a constant value" error in VS code for C

Chen Chang
  • 109
  • 1
  • 8
  • @Someprogrammerdude It's a intellisense error. The compiler works fine. – Chen Chang Dec 09 '21 at 12:52
  • @pmg Still the same error, "expression must have a constant value" on "N" – Chen Chang Dec 09 '21 at 12:59
  • @pmg Yes, but is that related to the intellisense issue? – Chen Chang Dec 09 '21 at 13:07
  • 2
    Does this answer your question? ["expression must have a constant value" error in VS code for C](https://stackoverflow.com/questions/67547361/expression-must-have-a-constant-value-error-in-vs-code-for-c) Please read the comments, too, and be aware that VCS's code checker can consider things differently than GCC. – the busybee Dec 09 '21 at 13:29
  • @thebusybee Thank you for replying. I did read the post before posting this question. However, the c/c++ extension doesn't seem to have an option for selecting between 'c' and 'c++'. It probably uses the file extension to determine which language to use, which in this case, is set to ".c". – Chen Chang Dec 09 '21 at 13:39
  • 2
    "It's a intellisense error" is exactly correct. Intellisense is wrong, especially for C99. This is very likely related to the fact that Microsoft's own C compiler has never supported variable-length arrays, and Microsoft seems uninterested in adding support for them, especially now that it is optional in C11 and later. More generally, Microsoft seems not to be interested in any part of C that cannot be construed as a subset of C++. – John Bollinger Dec 09 '21 at 14:00

2 Answers2

1

The IDE may be warning you of a potential programming error, since value N can be changed while mintree and treewalked are still in scope. You should be able to fix this with the following modification:

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

int main() {
    int N_scan, i, j;
    scanf("%d", &N_scan);
    const int N = N_scan;
    int min_tree[N][N];
    int tree_walked[N];
}

The value of N cannot be changed while min_tree and tree_walked are in scope.

There is a similar answer (for a C++ case) here:

https://stackoverflow.com/questions/9219712/c-array-expression-must-have-a-constant-value#:~:text=expression%20must%20have%20a%20constant%20value.%20When%20creating,declared%20const%3A%20doesn%27t%20even%20provide%20a%20variable%20name.

Jonathon S.
  • 1,928
  • 1
  • 12
  • 18
  • 3
    It does not matter for a C variable-length array whether the value of the expression designating the array length remains the same for the lifetime of the array. Only the value at the point where the array declaration appears matters. The array keeps that length for its lifetime. – John Bollinger Dec 09 '21 at 13:53
  • @JohnBollinger - I tried to be careful in the wording to use the term "potential programming error" rather than "compile error". My guess is that static analysis is identifying this as a "vulnerability" in the program. Using a `const`-qualified value ensures that the value may be accessed throughout the life of `min_tree` and `tree_walked` for identifying the array dimensions. – Jonathon S. Dec 09 '21 at 13:57
  • 1
    I find that explanation doubtful, especially if Intellisense is indeed flagging an *error*, as the OP claims, as opposed to a warning. A better explanation is that the rule for C++ arrays is being applied to C, or that VSCode simply denies the existence of VLAs in C. – John Bollinger Dec 09 '21 at 14:03
  • 1
    The extension didn't flag this as a "error" nor "warning". It prints this message in the "Problem" section. It's an annoying issue, but can be avoided using this answer. So I think it might be the best option we have. – Chen Chang Dec 09 '21 at 14:09
  • 1
    So if we use VSC we need to add an superfluous variable just to calm the erroneous code analyzer. Great. – the busybee Dec 09 '21 at 15:10
  • @thebusybee - If someone wanted to avoid the extra variable in this case, they could wrap `fraction[idx]` inside another function that return an integer that can be used to initialize `N`. This would be more abstract but may not add much benefit, since it adds even more code. – Jonathon S. Dec 09 '21 at 15:14
  • If I had to use VSC I would switch off the analyzer. The only true source of warnings and errors is the compiler (and linker). If I would need more, some decent code checker comes into play. -- Anyway, it confirms my image of Microsoft. ;-) – the busybee Dec 09 '21 at 15:18
  • "N can be changed while mintree and treewalked are still in scope" That doesn't matter once the arrays are declared. `const int N = N_scan;` is nonsense and by no means required by the C language. Check out [How to declare variable-length arrays correctly?](https://software.codidact.com/posts/283440) – Lundin Dec 10 '21 at 15:16
  • @Lundin - Just to clarify, I never indicated that this is required by the C language, which is why I used the term "potential programming error". Static analysis tools will identify problems that are not necessarily standard violations. In this case the message may be intended to prevent the developer from doing something unintentional, such as modifying `N` later while the arrays are still in scope, then using the modified `N` later. We all have preferences, but if the developer wants to use the IDE, but prevent the warning, this solution works and should have minimal impact on the build. – Jonathon S. Dec 10 '21 at 15:30
1

Just to add to the other answer, VS Code's intellisense defaults to MSVC as compiler. MSVC does not support the entirety of C99 including non-constant length arrays.

You can change the compiler used in VS Code's settings and set it to GCC which supports this feature.

In vs code, type "C/C++: Edit Configurations (UI)" (without the quotes) in the command palette (Ctrl + Shift + P) and edit the configuration. It will however create a .vscode/c_cpp_properties.json file in your folder. I don't think there is a way to set it globally other than copying this file to every .vscode folder.

screenshot

abhate
  • 95
  • 1
  • 6