Initialization of arrays in C

前端 未结 7 1054
难免孤独
难免孤独 2021-01-21 23:57

In C, I have read that half-initialized arrays will be filled with zeros for the rest of the elements (irrespective of integer or char arrays).

E.g.:

in         


        
7条回答
  •  星月不相逢
    2021-01-22 00:08

    Variables, located in data segment (global and unit scope), are automatically initialised to all zeros.

    Stack variables (function and block scope) are filled with garbage unless explicitly initialised, even partially initialised. In case of partial initialisation, reminder is zeroed.

    That's by the C standard and all compilers must adhere to it.

提交回复
热议问题