【发布时间】:2015-12-29 15:50:14
【问题描述】:
由于我目前不明白的原因,尽管拥有完成任务所需的一切,但此代码将无法编译。
#include <stdio.h> /* printf */
#include <math.h>
double resistance;
double voltage;
double current;
double wattage;
int main()
{
printf("type in resistance\n");
scanf("%f",resistance);
printf("type in current");
scanf("%f",current);
//voltage = resistance*resistance*current;
printf("%f Volts",resistance*resistance*current);
// return voltage;
}
我不明白的是为什么它不能编译?编译时我不断收到“错误的说明符”消息。我尝试了%lf 和%f,但它们都不起作用。
【问题讨论】:
-
仔细看第二个参数
scanf。 -
例如GCC 为您提供正确的信息:
x.c:13:7: warning: format '%f' expects argument of type 'float *', but argument 2 has type 'double' [-Wformat=] -
我不确定我是否明白,它们在我看来都一样。我认为您指的是 scanf("%f",current);
-
顺便说一句:
voltage = resistance*resistance*current是错误的。应该是voltage = resistance*current
标签: c scanf format-specifiers