Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
what does the statement
int x[10] = {0}; 
mean ?
what confuses me is that the arry is in size of 10 ,it means it must contain 10 elements ..but why we can set it to only one element
{0}


without to get an error ?
the language is c!

What I have tried:

...........................................
Posted
Updated 15-May-21 22:24pm

When initializing arrays in 'C', we do not need to specify all the elements in the array if we want them to be zero. By using
C
int x[10] = { 0 }
we can be assured that all 10 elements of the array are zero, without having to type out each one. This is handy if you have, say int i[500] = { 0 };
If we know that we want only the first few elements of the array assigned, we can also do
C
int x[10] = { 1, 2, 3 };
which produce an array of 10 ints with the first three elements assigned the values, 1, 2, 3, and the rest of the array initialized to zero. In C99 and later, we can be even default initialize only a few elements of an array using the following syntax:
C
int x[6] = { [3] = 18, [1] = 9 }; // equivalent to int x[5] = { 0, 9, 0, 18, 0, 0 }
 
Share this answer
 
Comments
Ahmad Qassym 15-May-21 17:12pm    
thanx!
another Qeustion:
when we say: char *name; is that pointer declaration or string declaration ?
k5054 15-May-21 17:46pm    
That's a pointer declaration.
This convenience syntax has become part of the C language reference in the C 99 standard to speed up development.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900