printf 要注意的小細節
先看看下面的程式碼,非常簡單
#include
int main()
{
int a = 28;
int b = 4;
double x = 2.8;
double y = 0.4;
printf( "a/b = integer~ %d\n", (a/b) );
printf( "a/b = float~ %f\n", (a/b) );
printf( "a/b = double~ %lf\n",(a/b) );
printf( "a/b = float~ %f\n", (double)(a/b) );
printf( "a/b = double~ %lf\n",(double)(a/b) );
printf( "x/y = integer~ %d\n", (int)(x/y) );
printf( "x/y = float~ %f\n", (x/y) );
printf( "x/y = double~ %lf\n", (x/y) );
return 0;
}
很簡單吧,但是顯示出來的結果可能出乎一些人的意料~
a/b = integer~ 7
a/b = float~ 0.000000
a/b = double~ 0.000000
a/b = float~ 7.000000
a/b = double~ 7.000000
x/y = integer~ 6
x/y = float~ 7.000000
x/y = double~ 7.000000
呵呵,為什麼呢~
太簡單了,不想回答~
但在這裡留下一個記錄,提醒不要犯下這種愚蠢的錯誤。
#include
int main()
{
int a = 28;
int b = 4;
double x = 2.8;
double y = 0.4;
printf( "a/b = integer~ %d\n", (a/b) );
printf( "a/b = float~ %f\n", (a/b) );
printf( "a/b = double~ %lf\n",(a/b) );
printf( "a/b = float~ %f\n", (double)(a/b) );
printf( "a/b = double~ %lf\n",(double)(a/b) );
printf( "x/y = integer~ %d\n", (int)(x/y) );
printf( "x/y = float~ %f\n", (x/y) );
printf( "x/y = double~ %lf\n", (x/y) );
return 0;
}
很簡單吧,但是顯示出來的結果可能出乎一些人的意料~
a/b = integer~ 7
a/b = float~ 0.000000
a/b = double~ 0.000000
a/b = float~ 7.000000
a/b = double~ 7.000000
x/y = integer~ 6
x/y = float~ 7.000000
x/y = double~ 7.000000
呵呵,為什麼呢~
太簡單了,不想回答~
但在這裡留下一個記錄,提醒不要犯下這種愚蠢的錯誤。
留言
張貼留言