【发布时间】:2020-05-24 07:39:17
【问题描述】:
我有以下代码:
#include <stdio.h>
#include <stdlib.h>
int compare(const void* a, const void* b)
{
const int* x = a, *y = b;
if (*x < *y)
return -1;
else if (*x == *y)
return 0;
else
return 1;
}
int main()
{
size_t n;
printf("Enter number of numbers: ");
scanf("%zu", &n); //<-- warnings here
int numbers[n];
for (size_t i = 0; i < n; i++)
{
printf("Enter number #%zu: ", i + 1); //NO warning here, printf works as expected
scanf("%d", &numbers[i]);
}
qsort(numbers, n, sizeof *numbers, compare);
for (size_t i = 0; i < n; i++)
printf("%d ", numbers[i]);
printf("\n");
system("pause"); //don't comment for this, I know it's bad
}
我从 GCC 编译器收到 2 个警告:
warning: unknown conversion type character 'z' in format [-Wformat=]
warning: too many arguments for format [-Wformat-extra-args]
我已将 GCC 设置为 C11。谁能帮我理解我为什么会得到这个?
编辑:我忘了提到我的代码仍然有效,即使有这些警告,但对我来说很奇怪,变量 n 似乎没有从 scanf 获取它的值
【问题讨论】:
-
你在用mingw吗?众所周知,MS C 运行时不支持许多 C99 功能。
-
mingw 显示这些警告。因此,如果您想使用它并具有可移植性 - 需要条件编译
-
我正在使用 MinGW,但我不知道如何检查我的编译器版本(如果我在命令提示符下运行
gcc --version,它会显示 2.95,所以我认为不是那个)。 -
@DarkAtom 这是正确的版本。 minGW simple 不支持。
-
@DarkAtom "如果我在命令提示符下运行 gcc --version 它会显示 2.95" 2.95 于 1999 年发布!你甚至在哪里找到它?去获取这个千年的版本。 :P