【发布时间】:2013-09-27 10:57:14
【问题描述】:
我对来自 gcc 编译器的警告消息有疑问。当 scanf 的参数不是指向应该携带用户输入的变量的指针时,会出现警告消息。
#include <stdio.h>
int main(int argc, const char *argv[]) {
float number;
scanf("%f", number); /* the value of 'number' is passed, instead of the adress to it */
return 0;
}
gcc 在编译程序时会给出如下警告信息。
scanf-problem.c: In function 'main':
scanf-problem.c:5:5: warning: format '%f' expects argument of type 'float *', but argument 2 has type 'double' [-Wformat=]
scanf("%f", number);
^
正如预期的那样,gcc 希望 scanf 的第二个参数具有“float *”类型(指向浮点数的指针)。困扰我的是 gcc 认为第二个参数的类型是“double”,而实际上它的类型是“float”。
这让我想到了一个问题,为什么 gcc 认为 scanf 的第二个参数是双精度的,而它实际上是一个浮点数?
我已经对此主题进行了一些研究以获得答案,但我找到的每个答案都是关于如何摆脱警告(写“&number”而不是“number”)。
【问题讨论】:
-
它基本上是scanf接受地址,如float *,并且您传递了8字节的数字,您是在64位PC上吗?
-
这解释了很多。事实上,我使用的是 64 位 PC。
标签: c gcc compiler-warnings scanf