float x = 12345.6f;
printf("%lf\n", x); // 輸出亂碼或 0
printf 是可變參函數,**默認參數提升**:float → double。%f 對應 double,**兼容** 提升後的 float。%lf 在 C99 標準中才正式用於 printf 表示 double(舊標準 %lf 未定義),部分舊編譯器(如 VC6、Turbo C)不支持,導致解析錯誤。float x = 12345.6f;
printf("%f\n", x); // 始終正確
printf("%.2f\n", x); // 保留2位小數
scanf 接收**地址**,無提升:
float f; scanf("%f", &f);double d; scanf("%lf", &d);printf 統用 %f;scanf 分 %f/%lf。