Is a string literal considered partial initializer and zero initialization?

In C, you can partially initialize a structure or array, as a result of which elements / elements that are not mentioned in the initializer are initialized to zero. (Section C99 6.7.8.19). For example: -

int a[4] = {1, 2}; // a[0] == 1 // a[1] == 2 // a[2] == 0 // a[3] == 0 

You can also initialize the "character type array" with a string literal (section C99 6.7.8.14), and "consecutive characters ... initialize the elements of the array". For example: -

 char b[4] = "abc"; // b[0] == 'a' // b[1] == 'b' // b[2] == 'c' // b[3] == '\0' 

Everything is pretty simple. But what happens if you explicitly specify the length of the array, but use a literal too short to fill the array? Are the remaining characters zero-initialized or undefined?

 char c[4] = "a"; // c[0] == 'a' // c[1] == '\0' // c[2] == ? // c[3] == ? 

Seeing it as a partial initializer would make sense, it would make char c[4] = "a" behave exactly like char c[4] = {'a'} , and it would have a useful side effect, allowing you with zero initialization of the array of the whole character with char d[N] = "" , but it is not at all clear to me that what the specification requires.

+12
c string initialization literals
Aug 02 2018-12-12T00:
source share
3 answers
  char c[4] = "a"; 

All other elements of the array will be set to 0 . That is, not only c[1] , but also c[2] and c[3] .

Please note that this does not depend on the duration of storage c , i. e. even if c has an automatic storage duration, the remaining items will be set to 0 .

From Standard C (Impact Impact):

(C99, 6.7.8p21) "If the list enclosed in curly brackets contains fewer initializers than the element or elements of the population, or fewer characters in the string literal , an array with a known size is used to initialize than there are elements in the array, the rest of the aggregate "must be initialized implicitly in the same way as objects with static storage duration. "

+11
Aug 02 2018-12-12T00:
source share

From the C99 standard (as already stated by ouah ):

If the list enclosed in curly brackets contains fewer initializers than elements or elements of the population or fewer characters in the string literal used to initialize an array of known sizes than there are elements in the array, the rest of the population must be initialized implicitly in the same way as objects having a static storage duration.

and

If an object with automatic storage duration is not initialized explicitly, its value is undefined. If an object with a duration of static storage is not initialized explicitly, then:

  • if it has a pointer type, it is initialized with a null pointer;
  • if it has an arithmetic type, it is initialized to zero (positive or unsigned);
  • if it is an aggregate, each member is initialized (recursively) in accordance with these rules;
  • if it is a union, the first named element is initialized (recursively) in accordance with these rules.

And char is an arithmetic type, so the remaining elements of the array will be initialized to zero.

+5
Aug 02 2018-12-12T00:
source share

Absolutely everywhere in C, it follows the principle of "all or nothing" for initialization. If you only partially initialize an aggregate, the rest of that aggregate gets zero initialization.

You could say that this is excessive and less optimal with strings, but thatโ€™s how it works in C.

+3
Aug 02 2018-12-12T00:
source share



All Articles