0

I used C in Visual Studio to make a code for a user to input size of array.

The code does not work in Visual Studio and gives errors.

But on a site like replit it works.

I don't understand what to do to make it work in Visual Studio.

enter image description here

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <math.h>

int main()
{
    int m;
    do
    {
        printf("please enter array size--> ");
        scanf_s("%d", &m);
    } while (m <= 1);

    int arry[m];

    for (int i = 0 + 1; i < m + 1; i++)
    {
        printf("%d,", arry[i] = i);
    }

    return 0;
}
the busybee
  • 10,755
  • 3
  • 13
  • 30
  • Don't post pictures of text, post text as properly formatted text. And indent your code properly. That being said, you're using VLAs (variable length arrays) which are not available with the Microsoft compiler. Quick and dirty fix: use a fixed size array of appropriate size like: `int arry[1000];` – Jabberwocky Aug 23 '22 at 09:38
  • 2
    Just pick a useful, standards compliant compiler instead of MSVC and it will compile just fine. – Lundin Aug 23 '22 at 10:00

3 Answers3

6

You can try my sanity check code below to test the usefulness and standard compliance of your compiler:

#include <stdio.h>

int main()
{
  #if !defined(__STDC__) || !defined(__STDC_VERSION__)
    puts("This compiler is garbage.");
  #elif (__STDC_VERSION__ >= 201112L)
     #if (__STDC_NO_VLA__==1)
       puts("This compiler is mighty strange but compliant.");
     #else
       puts("This compiler is modern and useful.");
       int m = 5;
       int array[m];
     #endif
  #elif (__STDC_VERSION__ == 199901L)
    puts("This compiler is old but useful.");
    int m = 5;
    int array[m];
  #endif

  return 0;
}

Compilers giving the output "mighty strange" or "garbage" will not support variable-length arrays, if supporting the C language at all.

Output from various common x86 compilers below.

Default settings

  • clang 14.0.0 x86: This compiler is modern and useful.
  • gcc 12.1 x86: This compiler is modern and useful.
  • icc 2021.5.0 x86: This compiler is modern and useful.
  • icx 2022.0.0 x86: This compiler is modern and useful.
  • MSVC 19.32 x86: This compiler is garbage.

-std=c99

  • clang 14.0.0 x86 -std=c99: This compiler is old but useful.
  • gcc 12.1 x86 -std=c99: This compiler is old but useful.
  • icc 2021.5.0 x86: -std=c99: This compiler is old but useful.
  • icx 2022.0.0 x86: -std=c99: This compiler is old but useful.
  • MSVC 19.32 x86 /std:c99: This compiler is garbage.

-std=c11

  • clang 14.0.0 x86 -std=c11: This compiler is modern and useful.
  • gcc 12.1 x86 -std=c11: This compiler is modern and useful.
  • icc 2021.5.0 x86 -std=c11: This compiler is modern and useful.
  • icx 2022.0.0 x86 -std=c11: This compiler is modern and useful.
  • MSVC 19.32 x86 /std:c11: This compiler is garbage.

-std=c17

  • clang 14.0.0 x86 -std=c17: This compiler is modern and useful.
  • gcc 12.1 x86 -std=c17: This compiler is modern and useful.
  • icc 2021.5.0 x86 -std=c17: This compiler is modern and useful.
  • icx 2022.0.0 x86 -std=c17: This compiler is modern and useful.
  • MSVC 19.32 x86 /std:c17: This compiler is garbage.
Lundin
  • 195,001
  • 40
  • 254
  • 396
2

This declaration

int arry[m];

is a declaration of a variable length array that is conditionally supported by C compilers because m is not an integer constant expression.

It seems you are using a version of MS VS the C compiler of which does not support variable length arrays or you need to change properties of your project and select the last version of the C compiler.

Otherwise declare an array of a fixed size that is not less than the possible value of the variable m or allocate it dynamically.

In any case pay attention to that this for loop

for (int i = 0 + 1; i < m + 1; i++)
{

printf("%d,", arry[i] = i);

}

results in accessing memory beyond the array because the valid range of indices is [0, m).

It must look like

for (int i = 0; i < m; i++)
{
    printf("%d,", arry[i] = i + 1 );
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I deleted my comment when I saw the next answer saying exactly what I did. You could possibly get the MS bundled clang compiler to handle the C code, so in a way, yes. – BoP Aug 23 '22 at 12:00
  • @BoP No I am using the MS VS compiler. The MS VS compiler has a bug. It incorrectly shows the version. Nevertheless the MS VS 19 compiler supports VLAs. – Vlad from Moscow Aug 23 '22 at 12:03
0

By default, when MSVC compiles code as C, it implements ANSI C89 with Microsoft-specific language extensions. Some of these MSVC extensions are standardized in ISO C99 and later.

__STDC_NO_VLA__ is defined as 1 if the implementation doesn't support standard variable length arrays. The MSVC implementation defines it as 1 when compiled as C and one of the /std C11 or C17 options is specified.

https://clang.llvm.org/c_status.html#c99

https://gcc.gnu.org/c99status.html

MS says VLA is dangerous for you & less efficient so they are not supported

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Wr0NG
  • 26
  • 3
  • 2
    Compilers not defining `__STDC__` to indicate compilance can't be trusted to correctly implement any other `__STDC__` prefixed macros either. It would mean that they are either C89 implementations without the C95 addendum, or that they are not compliant to anything, what-so-ever. – Lundin Aug 23 '22 at 11:18
  • @Lundin `__STDC_NO_VLA__` is only feature test predefined macro (in C11 VLA is optional) to determine whether an implementation supports a certain feature or not – Wr0NG Aug 23 '22 at 11:35
  • 2
    But why would an application define `__STDC_NO_VLA__` if it doesn't define `__STDC__`? That would be senseless. – Lundin Aug 23 '22 at 12:02
  • @Lundin to check `__STDC_NO_VLA__` supports :) When both C compilation and the /Za option are specified, the C compiler conforms strictly to the C89/C90 standard. The compiler treats M$ extended keywords as simple identifiers, disables the other M$ extensions, and automatically defines the `__STDC__` predefined macro for C programs. – Wr0NG Aug 23 '22 at 12:15
  • A tangential, borderline-rant discussion about C language standards, portability, and Microsoft has been [moved to chat](https://chat.stackoverflow.com/rooms/247854/discussion-on-answer-by-zlw-expression-must-have-a-constant-value-problem). – Cody Gray - on strike Sep 07 '22 at 18:02