分配零在C中浮动 [英] Assign zero to float in C

查看:182
本文介绍了分配零在C中浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我养成了使用float f = 0的习惯。 //将一个零值赋给C中的一个浮点数


我应该使用float f = 0.f; / /明确的浮点大小或只是停止搞乱和使用浮动f = 0; / /没有跟踪任何东西?



我从哪里拿起这种习惯,为什么?



是任何版本比任何其他的更正确或错误?

解决方案

0.0 and 0。是双打,而不是浮动。虽然在没有明确的转换的情况下将双精度浮点数分配到C中是合法的,但是 0.0f 或者 0.f 应该是正确的方法。 0 是一个整数,因此同样是错误的,但它也会'工作'。


I've got in the habit of using float f = 0.; //with a trailing period when assigning a zero value to a float in C.

Should I be using float f = 0.f; //with an explicit float size or just stop messing about and use float f = 0; //with no trailing anything?

Where did I pick up that habit and why?

Is any version more right or wrong than any other?

解决方案

0.0 and 0. are doubles, not floats. While it is legal to assign doubles to floats in C without an explicit cast, 0.0f or 0.f would be the correct way. 0 is an integer and thus equally wrong, but it will also 'work'.

这篇关于分配零在C中浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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