C语言输入输出浮点数问题:为什么输入283.19却输出0.00?
#include <stdio.h>
int main(){ float c; scanf("%f", &c); printf("%10.4f", c); return 0; }
这段代码存在两个问题导致输出不符合预期:
-
在
scanf函数中,格式控制字符串%f表示读取一个浮点数,可以读取任意长度的浮点数。 -
在
printf函数中,格式控制字符串%10.4f表示输出一个浮点数,其中总共10个字符,其中包括4个小数位。
修改后的代码:
#include <stdio.h>
int main() {
float c;
scanf("%f", &c);
printf("%10.4f", c);
return 0;
}
现在,当你输入 '283.19' 时,程序会输出 '283.1900',符合你的预期结果。
原文地址: https://www.cveoy.top/t/topic/Q2o 著作权归作者所有。请勿转载和采集!