0

Initialization of float can be done as follows,

float a = 0.0
float a = 0.f
float a = float(0)

Is there any pros and cons to use any of these?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Adam Lee
  • 24,710
  • 51
  • 156
  • 236
  • possible duplicate of [What is the significance of 0.0f when initializing (in C)?](http://stackoverflow.com/questions/5199338/what-is-the-significance-of-0-0f-when-initializing-in-c) – user1810087 Mar 22 '14 at 07:52

2 Answers2

1

It doesn't matter at all. You could also say float a = 0; and again it would be the same thing. Or float a = float();. I think the most conventional would be 0, 0.0, or 0f; the rest are just redundant.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

The first initializes from a double literal, whereas the second is from a float literal. The bits in the two zeros may not be the same.

The third is c++ constructor-like syntax that actually just does direct initialization, in this case from an integer literal.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • The bits in a float initialized from a double 0 might differ from one directly from a float 0? How? IEEE 754 has only two ways to represent zero as far as I know, and one is -0 which neither of the above will give you. – John Zwinck Mar 22 '14 at 06:54