Default variable value

后端 未结 9 1710
生来不讨喜
生来不讨喜 2020-11-22 13:21

If I don\'t assign a value to a variable when I declare it, does it default to zero or just whatever was previously in the memory?

e.g.

float x;
         


        
9条回答
  •  独厮守ぢ
    2020-11-22 13:36

    It depends on the lifetime of the variable. Variables with static lifetime are always zero-initialized before program start-up: zero-initialization for basic types, enums and pointers is the same as if you'd assigned 0, appropriately converted to the type, to it. This occurs even if the variable has a constructor, before the constructor is called.

提交回复
热议问题