Jack Zha
Jack Zha

Reputation: 1

Arrays-Problem with global integer declaration in C

Completely new to programming, currently following along the CS50 course on Youtube, so i don't have access to their CS50 sandbox. I'm currently on lecture 2 about the arrays. According to the professor, the following codes should compile and run just fine, but on the Visual Studio 2019 Community edition on my PC, I have run into error C2057, expected constant expression; and error c2466, cannot allocate an array of constant size 0, when compiling.

Please help explain why in CS50 online sandbox, these codes are fine, while in VS 2019 they are not? Are my codes not written correctly, or is it VS2019 not working the same way as CS50 sandbox? I read somewhere VS2019 is not compatible with C99 or something like that, is this related to the issue at hand?

PS: also read today that VS2019 will support C11 and C17, will this new update help?

Thanks a lot!

These are my codes:

#include <stdio.h>

const int N = 5;

int main(void)
{
    double scores[N] = { 59, 60, 90, 85, 87 };
    double total_score = 0;
    for (int i = 0; i < N; i++)
    {
        
        total_score += scores[i];
    }
    double average = total_score / N;
    printf("Average score is : %.1f,", average);
}

Upvotes: 0

Views: 125

Answers (3)

Some programmer dude
Some programmer dude

Reputation: 409196

This problem is actually a mix of things.

First of all const variables aren't actually compile-time constants, they are just run-time constants.

The second thing is that while C does support variable-length arrays (arrays whose size are set at run-time), such arrays can't be initialized.

So either you need to have a proper compile-time constant for the array size, usually defined as a macro, and then you can create an initialized array as in your program. Or you can have a variable-length array but then you can't initialize it.

It seems here that the compiler used by the CS50 sandbox is using some kind of extension to either make const qualified variables compile-time constants, or it allows initialization of variable-length arrays.

Visual Studio and its compiler on the other hand, sees the initialization and the make the decision that it can't be a variable-length array, in which case the size must be a compile-time constant. Since it's not it will emit an error.

Upvotes: 1

dbush
dbush

Reputation: 224062

Because the array size N is a variable and not an integer constant, the array scores is a variable length array (VLA). Such arrays are part of the C99 standard and an optional feature of the C11 standard.

However, Visual Studio does not support VLAs. You'll need to either put the value in place directly or use a macro instead of a variable:

#define N 5

It's important to note here that a variable with the const qualifier does not qualify as a constant expression.

Upvotes: 1

The code requires the C99 feature variable length arrays, because, even though const in const int N means constant, it is not actually considered a compile-time constant in C.

Unfortunately even very recently MSVC compilers have implemented only the 30-year-old (!!) ISO C90 standard revision with some extensions, but not the variable length arrays.

You can fix this program to be C89 by changing the const int N to a preprocessor macro:

#define N 5

That VS2019 would support C11, C17 won't help you one bit, because due to Microsoft's refusal to ever implement variable-length arrays from C99, VLAs have been made optional in C11, C17, and it has been stated that MSVC will not implement them.

Upvotes: 2

Related Questions