关于网友提出的“ C语言printf的问题”问题疑问,本网通过在网上对“ C语言printf的问题”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: C语言printf的问题
描述:c语言printf
#include
#define PAGES 931
int main()
{
const double RENT=3852.99;
printf("*%d*\n",PAGES);
printf("*%2d*\n",PAGES);
printf("*%10d*\n",PAGES);
printf("*%-10d*\n",PAGES);
printf("*%f*\n",RENT);
printf("*%e*\n",RENT);
printf("*%4.2f*\n",RENT);
printf("*%3.1f*\n",RENT);
printf("*%10.3e*\n",RENT);
printf("*%+4.2f*\n",RENT);
printf("*%010.2f*\n",RENT);
return 0;
}
输出是这样的。
倒数第三行的输出 书上是* 3.863e+03*,请各位给我解释一下,谢谢大家。
解决方案1: * 3.863e+03*是符合ANSI C标准的输出。你使用的编译器不符合标准(不会是臭名昭着的VC6吧?)
按照标准定义
%10.3e
10表示整个输出有10个字符,不足10个时前面用空格补齐补齐(7.19.6.1.4 If the converted value has fewer characters than the
field width, it is padded with spaces (by default) on the left)
3表示小数点后面有3位(7.19.6.1.4 An optional precision that gives the minimum number of digits to appear)
e的指数部分一般是2位,只有指数值大于99时才会占3位(7.19.6.1.8 The exponent always contains at least two digits,
and only as many more digits as necessary to represent the exponent.)
你用的编译器在指数值等于3时占了3位与标准不一致。
解决方案2: 书上说的也没错,Linux下输出确实是
* 3.853e+03*
以上介绍了“ C语言printf的问题”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2568014.html