在 C 中将零赋给浮点数
我养成了使用 float f = 0 的习惯。 //带有尾随句点 当为 C 中的浮点数分配零值时。
我应该使用 float f = 0.f; //使用明确的浮点大小或者停止搞乱并使用 float 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
0.0
和0.
是双精度数,而不是浮点数。虽然在 C 中将双精度数分配给浮点数而不进行显式转换是合法的,但0.0f
或0.f
将是正确的方法。0
是一个整数,因此同样是错误的,但它也可以“工作”。0.0
and0.
are doubles, not floats. While it is legal to assign doubles to floats in C without an explicit cast,0.0f
or0.f
would be the correct way.0
is an integer and thus equally wrong, but it will also 'work'.您所需要的只是
float f = 0;
并且没有尾随句点。如果尾随句点和/或尾随 f 或其他任何内容使代码从您的角度更容易理解,那么请务必使用它。使用最适合您和您的同事的方法。
All you need is
float f = 0;
and no trailing period.If the trailing period and/or the trailing f or whatever makes the code easier to understand from your perspective then by all means use that. Use what works best for you and your co-workers.
尾随 .或 .f 只是为了增加可读性。
float f = 0;
就足够了trailing . or .f is just to increase readability.
float f = 0;
is enough