Static variable initialization?

后端 未结 7 1112
北海茫月
北海茫月 2020-11-30 08:05

I want to know why exactly static variables in C, C++ and Java are initialized by zero by default? And why this is not true for local variables?

7条回答
  •  庸人自扰
    2020-11-30 08:41

    I have no idea about java and I doubt it's different for statics/locals in java.

    As for c and c++, it's about programmers caring about their code effect and loving being in control. Initializing local variables would imply execution of extra code each time program enters the scope. For frequently called functions that may be a disaster.

提交回复
热议问题