What happens if the number of characters entered in your character array is less than the number of spaces you specify

char ch_arry[20] = { S, A, L, L, Y};
printf( "%s", ch_arry[] );

Will it fill the end of the array with null characters?

+4
source share
3 answers

From standard C (6.7.9 Initialization)

19 Initialization must be performed in the order of the list of initializers, each initializer provided for a particular subobject, the previously specified initializer for the same subobject; 151) all subobjects that are not initialized explicitly must be implicitly initialized the same as objects with a static storage duration .

and

10 , . , , :

- , ;

- , , ( ) ;

- , () ,, ;

,

char ch_arry[20] = { 'S', 'A', 'L', 'L', 'Y'};

, , .

+6

15 \0. C.

,

char ch_arry[20] = { 'S', 'A', 'L', 'L', 'Y'};

,

printf("%s", ch_arry);
+4

.

the remaining 15 characters will be set to zero.

And you declare the array incorrect ... if you want ch_array = SALLY, you must use one inverted comma ('S' like this) with each of your characters.

For output, use printf ("% s", ch_arry); instead of printf ("% s", ch_arry []);

0
source

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


All Articles