【发布时间】:2013-11-07 04:11:01
【问题描述】:
我看到两个程序的结果不同,我希望产生相同的输出,第一种情况:
int money;
printf("Enter the price of the car: ");
scanf("%d", &money);
printf("\nResult: %d .\n", money+money*0.4);
第二种情况:
int money;
printf("Enter the price of the car: ");
scanf("%d", &money);
money=money+money*0.4;
printf("\nResult: %d .\n", money );
return 0;
在第一种情况下,printf 的结果是0,但在第二种情况下不是。为什么我会看到这些不同的结果?
【问题讨论】:
-
您应该启用警告才能理解。在第一种情况下是
format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’在第二种情况下是conversion to ‘int’ from ‘double’ may alter its value。我想警告很好地解释了自己。
标签: c type-conversion printf undefined-behavior