#include <stdio.h>
main(void)
{
struct computer
{
float cost;
int year;
int cpu_speed;
char cpu_type[16];
} model;
printf(“The type of the CPU inside your computer?n”);
gets(model.cpu_type);
printf(“The speed(MHz) of the CPU?n”);
scanf(“%d”, &model.cpu_speed);
printf(“The year your computer was made?n”);
scanf(“%d”, &model.year);
printf(“How much you paid for the computer?n”);
scanf(“%f”, &model.cost);
printf(“Here are what you entered:n”);
printf(“Year: %dn”, model.year);
printf(“Cost: $%6.2fn”, model.cost);
printf(“CPU type: %sn”, model.cpu_type);
printf(“CPU speed: %d MHzn”, model.cpu_speed);
return 0;
}
上面的代码来自 24 小时内自学 C,但它在运行时显示杂散错误。
在书中,还显示了输出。
在输出中,模型的成本是$1234.56
。1234.56
如何适应%6.2f
...我的意思是%6.2f
我们只会得到234.56
,对吧?
双引号“
和”
的字符无效,导致杂散错误。您可能让他们从其他网站或 pdf 复制它们。否则,您需要检查键盘设置。
正确的双引号是 "
。
%6.2f
中的 6 是最小字段宽度。
你对字符串使用了奇怪的字符:你用了“
但你必须用"