What is the significance of 0.0f when initializing (in C)?

前端 未结 7 521
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 04:30

I\'ve seen code where people initialize float variables like this:

float num = 0.0f;

Is there a significant difference between this and jus

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 05:03

    Paul R has written the answer. Your second example has an integer initialization value.

    You should always use an initializer of the same type as the variable that is being initialized. This avoids any conversion at compile time (ideally) or run time (lazy compilers: are any this lazy, though?). And perhaps more importantly, conversion can lead to some strange things in the general case.

    Here conversion should do exactly what is expected, but it is still good style and avoids a compiler warning.

提交回复
热议问题