Static variable initialization?

后端 未结 7 1107
北海茫月
北海茫月 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:45

    Paragraph 8.5.6 of the C++ standard states that:

    "Every object of static storage duration shall be zero-initialized at program startup"

    (The standard also says that the initialization of local variables is undefined)

    As to why, the standard doesn't say ;) One guess is that it's reasonably easy to implement without any additional downsides.

提交回复
热议问题