zell
zell

Reputation: 10204

Does "char input[10]={0}" initialize all elements?

I think that this C statement:

 char input[10]={0}

assigns the first char of input to 0, and the remaining chars in the array to NULL. Does this happen with all C compilers?

Upvotes: 2

Views: 598

Answers (3)

Rohit Sharma
Rohit Sharma

Reputation: 1

it store NULL value in all position of array.

Upvotes: -3

Sourav Ghosh
Sourav Ghosh

Reputation: 134336

Yes, if you're using a partial initializer list for an array type, the remaining elements are initialized as if they were having static storage.

Quoting C11, chapter §6.7.9, Initialization, Paragraph 21

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

and, from Paragraph 10,

If an object that has static or thread storage duration is not initialized explicitly, then:

— if it has pointer type, it is initialized to a null pointer;

— if it has arithmetic type, it is initialized to (positive or unsigned) zero;

— if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

— if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

In this case, this is arithmetic type, so the elements will have values as 0.

Upvotes: 3

dbush
dbush

Reputation: 223917

The statement given explicitly initializes the first element to 0 and implicitly sets the remaining elements to 0.

Section 6.7.9 of the C standard details this behavior:

10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread storage duration is not initialized explicitly, then:

  • if it has pointer type, it is initialized to a null pointer;
  • if it has arithmetic type, it is initialized to (positive or unsigned) zero;
  • if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
  • if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

...

21 If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

Paragraph 21 states that if an initializer doesn't have enough elements to initialize the variable, the remaining values are initialized just like a variable with static storage duration (i.e. a variable at file scope, or a local variable declared as static).

As far as how something with static storage is initialized, paragraph 10 states that an arithmetic type (which char is) is initialized with the value 0. This is not the same as NULL which is a null pointer constant, however on many implementations it has the same value.

So yes, any comforming C compiler with initialize in this way.

Upvotes: 7

Related Questions