Assign zero to float in C

纵然是瞬间 提交于 2019-12-12 01:29:59

问题


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?


回答1:


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'.




回答2:


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.




回答3:


trailing . or .f is just to increase readability.

float f = 0; is enough



来源:https://stackoverflow.com/questions/3422315/assign-zero-to-float-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!