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.
#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;
}
CodePudding user response:
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.
CodePudding user response:
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 );
}
CodePudding user response:
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__
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
M$ says VLA is dangerous for you & less efficient so they are not supported