Default variable value

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

    C++ does not instantiate variables. The value of x is whatever happened to be in the memory at the time. Never assume anything about its initial value.

提交回复
热议问题