printf("%f")问题 [英] printf("%f") question

查看:75
本文介绍了printf("%f")问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们想用printf打印浮点数,则打印一个printf(%f,x);

足够,或者像printf一样需要强制转换(%f,(double)x); ?

解决方案

澄清:问题是关于C90 / C95。


Ioannis Vranos写道:


如果我们想用printf打印一个浮点数,一个printf(%f,x);

足够,或者像printf一样需要强制转换(%f,(double)x); ?


On Sun,2008年3月2日15:16:11 +0200,Ioannis Vranos写道:


如果我们想用printf打印一个浮点数,则打印一个printf(%f,x);

足够,或者像printf一样需要强制转换(%f,(double)x); ?



函数的变量参数的任何浮点参数将被提升为double的
。两者都很好。


Harald van Dijk写道:


函数变量参数的任何浮点参数将

晋升为双倍。两者都很好。



我认为以下内容也是正确的:


signed char sc = 15;

unsigned char uc = 130;


printf("%d\t%hu \t%u",sc,uc,uc);


If we want to print a float with printf, a printf("%f", x); is
sufficient, or a cast is needed like in printf("%f", (double)x); ?

解决方案

Clarification: The question is about C90/C95.

Ioannis Vranos wrote:

If we want to print a float with printf, a printf("%f", x); is
sufficient, or a cast is needed like in printf("%f", (double)x); ?


On Sun, 02 Mar 2008 15:16:11 +0200, Ioannis Vranos wrote:

If we want to print a float with printf, a printf("%f", x); is
sufficient, or a cast is needed like in printf("%f", (double)x); ?

Any float argument to the variable arguments of a function will be
promoted to double. Both are fine.


Harald van Dijk wrote:

Any float argument to the variable arguments of a function will be
promoted to double. Both are fine.


I suppose the following are also correct:

signed char sc= 15;
unsigned char uc= 130;

printf("%d\t%hu\t%u", sc, uc, uc);


这篇关于printf("%f")问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆