views:

54

answers:

3

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  A: 

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.

John
It works, but on paper, it introduces an unnecessary cast. Modern compilers will probably polish it away, but technically speaking, 0.0f is the correct way.
tdammers
@tdammers: Yes, I agree. Unless I hit a noticable performance issue I prefer readability over the performance benefit.
John
I'd consider 0.0f the most readable, but then, I'm a bit of a zealot with these details.
tdammers
@tdammers: Whatever works best for you! ;-)
John
+1  A: 

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

tdammers
+1  A: 

trailing . or .f is just to increase readability.

float f = 0; is enough

Rahul