Int array [30] = {0}; How does it work in c?

How does it work (sets all values ​​to 0)?

int array[28]= {0};

and why doesn’t it work (does not set all values ​​to 4, but only the first value sets 4, and the others set 0)?

int array[28]= {4};
+4
source share
4 answers

Elements that are not initialized are set to 0. In the first case, you initialize it by giving it a value of 0, and the remainder defaults to initializing to 0. In your second case, the first values ​​are initialized to 4 and the rest as 0. The standard says:

, , , , , , , , .

+10

C , , .

int array[28]= {0}; 28 ints 0. , , 0 ints.

int array[28]= {4}; . 4, , , .

+10

, .

ISO/IEC: 9899 ( c99) TC3:

6.7.8

[...]

21 , , , , , , , , .

10: ( )

10 , . ,

- , ;

- , ( ) ;

- , () ;

- , () .

, , .

+2
int array[5] = {0}

0

int array[5] = {1,2,3}

1, , 3 0.

+1

Source: https://habr.com/ru/post/1610257/


All Articles