C语言中的%f和%lf搞不明白

dazitou123 2012-02-04 05:11:36
double d=12.123456789;
printf("%12.9f",d);
很精确的输出了12.123456789一个字不漏

但问题是%f不是对应单精度的吗?不是只能保证6位小数位的吗?为啥这里能用%f输出9位小数。。。。。彻底搞晕了
...全文
15112 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
mymtom 2012-02-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mstlq 的回复:]

printf中,%f通杀单精度和双精度
在scanf中,%f和%lf才有区别
[/Quote]

在C89中明确说明
printf("%lf", (doube)d);
的结果是未定义的。
An optional h specifying that a following d, i, o, u, x, or X conversion specifier applies to a short int or unsigned short int argument (the argument will have been promoted according to the integral promotions, and its value shall be converted to short int or unsigned short int before printing); an optional h specifying that a following n conversion specifier applies to a pointer to a short int argument; an optional l (ell) specifying that a following d, i, o, u, x, or X conversion specifier applies to a long int or unsigned long int argument; an optional l specifying that a following n conversion specifier applies to a pointer to a long int argument; or an optional L specifying that a following e, E, f, g, or G conversion specifier applies to a long double argument. If an h, l, or L appears with any other conversion specifier, the behavior is undefined.

C99中则说
printf("%lf", (doube)d);
相当于
printf("%f", (doube)d);

l (ell) Specifies that a following d, i, o, u, x, or X conversion specifier applies to a
long int or unsigned long int argument; that a following n conversion specifier applies to a pointer to a long int argument; that a following c conversion specifier applies to a wint_t argument; that a following s conversion specifier applies to a pointer to a wchar_t argument; or has no effect on a following a, A, e, E, f, F, g, or G conversion specifier.
飞天御剑流 2012-02-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 dazitou123 的回复:]

引用 4 楼 mstlq 的回复:

printf中,%f通杀单精度和双精度
在scanf中,%f和%lf才有区别



这是为什么呢?
[/Quote]
是由于历史原因造成的。

在标准C出现之前的K&R C中,从float到double的提升是一定进行的,K&R C将printf的float实参都提升到double后入栈。到了标准C,从float到double的提升被修改为不再一定进行,旧代码在新编译器中的编译就会产生问题,为了保持兼容性,K&R C的这条规则就被保留下来了。
Lactoferrin 2012-02-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lile1234_show 的回复:]

引用 5 楼 dazitou123 的回复:

引用 4 楼 mstlq 的回复:

printf中,%f通杀单精度和双精度
在scanf中,%f和%lf才有区别



这是为什么呢?

查查资料:printf 的 %f 标识符的确既可以输出浮点数又可以输出双精度数。根据 ``缺省参数扩展" 规则, 不论范围内有没有原形都会在在类似 printf 的可变长度参数列表中采用……
[/Quote]
这个不错
pathuang68 2012-02-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mstlq 的回复:]

printf中,%f通杀单精度和双精度
在scanf中,%f和%lf才有区别
[/Quote]

正解
RLib 2012-02-04
  • 打赏
  • 举报
回复 1
[Quote=引用 4 楼 mstlq 的回复:]

printf中,%f通杀单精度和双精度
在scanf中,%f和%lf才有区别
[/Quote]

膜拜版主

5L看文档
goodwithcode 2012-02-04
  • 打赏
  • 举报
回复
double对应%lf
float对应%f
12是数的所有位数,9是小数点后面的位数,嘿嘿,这个以前学过
lee_鹿游原 2012-02-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 dazitou123 的回复:]

引用 4 楼 mstlq 的回复:

printf中,%f通杀单精度和双精度
在scanf中,%f和%lf才有区别



这是为什么呢?
[/Quote]
查查资料:printf 的 %f 标识符的确既可以输出浮点数又可以输出双精度数。根据 ``缺省参数扩展" 规则, 不论范围内有没有原形都会在在类似 printf 的可变长度参数列表中采用, 浮点型的变量或扩展为双精度型, 因此 printf() 只会看到双精度数。
scanf,它接受指针,这里没有类似的类型提升。(通过指针)向float存储和向double存储大不一样,因此,scanf区别%f和%lf.
dazitou123 2012-02-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mstlq 的回复:]

printf中,%f通杀单精度和双精度
在scanf中,%f和%lf才有区别
[/Quote]


这是为什么呢?
mstlq 2012-02-04
  • 打赏
  • 举报
回复
printf中,%f通杀单精度和双精度
在scanf中,%f和%lf才有区别
sagegz 2012-02-04
  • 打赏
  • 举报
回复
[Quote=引用楼主 dazitou123 的回复:]
double d=12.123456789;
printf("%12.9f",d);
很精确的输出了12.123456789一个字不漏

但问题是%f不是对应单精度的吗?不是只能保证6位小数位的吗?为啥这里能用%f输出9位小数。。。。。彻底搞晕了
[/Quote]
这个printf里面有修饰符
12.9
12代表字段宽度的最小值
.后面的9表示精度
%12.9f打印一个浮点数,它的字段宽度为12个字符,小数点后面有9个数字
lee_鹿游原 2012-02-04
  • 打赏
  • 举报
回复


你的代码 比如%12.9f 点前面的12代表输出的数的宽度,也就是位数~包括小数点后的数字;点后的9代表小数点后面的数字的个数 。 明白 和%f单精度没关系

sagegz 2012-02-04
  • 打赏
  • 举报
回复
%f 浮点数(包括float和double)

65,209

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧