Default variable value

后端 未结 9 1702
生来不讨喜
生来不讨喜 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:59

    It depends. If this is a local variable (an object with automatic storage duration) it will be uninitialized, if it is a global variable (an object with static storage duration) it will be zero initialized. Check also this answer.

提交回复
热议问题