art*_*kin 0 c compiler-errors visual-studio constant-expression variable-length-array
我在 Visual Studio 中使用 C 编写了一个代码,供用户输入数组的大小。
该代码无法在 Visual Studio 中运行并给出错误。
但在像 replit 这样的网站上它是有效的。
我不明白如何才能使其在 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;
}
Run Code Online (Sandbox Code Playgroud)
您可以尝试下面的健全性检查代码来测试编译器的实用性和标准合规性:
#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;
}
Run Code Online (Sandbox Code Playgroud)
如果编译器完全支持 C 语言,则输出“非常奇怪”或“垃圾”将不支持可变长度数组。
下面是各种常见 x86 编译器的输出。
默认设置
This compiler is modern and useful.This compiler is modern and useful.This compiler is modern and useful.This compiler is modern and useful.This compiler is garbage.-std=c99
-std=c99:This compiler is old but useful.-std=c99:This compiler is old but useful.-std=c99:This compiler is old but useful.-std=c99::This compiler is old but useful./std:c99:This compiler is garbage.-std=c11
-std=c11:This compiler is modern and useful.-std=c11:This compiler is modern and useful.-std=c11:This compiler is modern and useful.-std=c11:This compiler is modern and useful./std:c11:This compiler is garbage.-std=c17
-std=c17:This compiler is modern and useful.-std=c17:This compiler is modern and useful.-std=c17:This compiler is modern and useful.-std=c17:This compiler is modern and useful./std:c17:This compiler is garbage.| 归档时间: |
|
| 查看次数: |
488 次 |
| 最近记录: |