问题
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