為什么結果和口算的不一樣
#include <stdio.h>
int main()
{
? ? int a,b,c,d;
? ? double result;
? ? a = 1;
? ? b = 2;
? ? c = 3;
? ? d = 4;
? ? result = a + b * c / d; ? ?//在這里體驗哦~
? ? printf("%f\n", result);
? ? return 0;
}
結果是2.000000
#include <stdio.h>
int main()
{
? ? int a,b,c,d;
? ? double result;
? ? a = 1;
? ? b = 2;
? ? c = 3;
? ? d = 4;
? ? result = a + b * c / d; ? ?//在這里體驗哦~
? ? printf("%f\n", result);
? ? return 0;
}
結果是2.000000
2015-10-09
舉報
2015-10-10
a b c d都是整型變量,它們間的加減乘除的結果也是整型(沒有小數(shù)部分)。 雖然result是浮點數(shù),但用整型值給它賦值,當然也不會有小數(shù)部分啦
2015-10-10
好好看看int的‘/’和float的‘/’有何區(qū)別,這個值是對的,就是2.000000