C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - Find Output of Program (Q.No. 9)
9.
What is the output of the program
#include<stdio.h>
int main()
{
    int a[5] = {2, 3};
    printf("%d, %d, %d\n", a[2], a[3], a[4]);
    return 0;
}
Garbage Values
2, 3, 3
3, 2, 2
0, 0, 0
Answer: Option
Explanation:
When an automatic array is partially initialized, the remaining elements are initialized to 0.
Discussion:
38 comments Page 1 of 4.

Raghav Naganathan said:   1 decade ago
@Bala and Jyoti...The program will print garbage values only if there has been no initialization done. In this case, you see that there is some partial initialization that has taken place, case in point: a[5]= {2,3}; which means that a[0]=2 and a[1]=3.

Once this initialization has been done, the values of a[2],a[3] and a[4] will all be initialized to 0.

Hope this helps.

Saga jarvis said:   4 years ago
Here the in the array only first to element are initialized (ie. Partially) , so in this;

Case 1: compiler automatically initialize to zero.
Case 2: if none of the are initialized then it would be the garbage values.
Case 3: If all are initialized then it will print as the normal way (ie. How they initialized in that way).
(1)

Saga jarvis said:   4 years ago
Here the in the array only first to element are initialized (ie. Partially) , so in this;

Case 1: compiler automatically initialize to zero.
Case 2: if none of the are initialized then it would be the garbage values.
Case 3: If all are initialized then it will print as the normal way (ie. How they initialized in that way).
(2)

Priya said:   9 years ago
#include<stdio.h>
int main()
{
int a[3] = {2, 3};
printf("%d, %d,%d\n", a[2], a[3],a[4]);
return 0;
}

Output for above program is like this: [0,-1612,-2697].

Why compiler giving garbage value if it is partially initialized?

Please explain and correct me if I am wrong..!

Purnima said:   1 decade ago
HERE a[5]=(2,3) means

a[0]=2 and a[1]=3 then fora[2],a[3],a[4] are not having values that means we are not giving initialization to those values.

So those values are automatically intialised to zero when we are calling to see the out put it will display as 0,0,0....

Correct me if I'm wrong ?

Vidya said:   1 decade ago
#include<stdio.h>
int main()
{

int a[6] = {2, 3};
printf("%d, %d, %d\n",a[0],a[1], a[2], a[3], a[4],a[5]);
return 0;
}
Then the output is 2 3 0.

That's it according to discussion here the output should be
2 3 0 0 0 0 right?

Please anyone explain me?

Sayeed said:   7 years ago
HERE a[5]=(2,3) means;

a[0]=2 and a[1]=3 then fora[2],a[3],a[4] are not having values that means we are not giving initialization to those values.

So, those values are automatically initialised to zero when we are calling to see the output it will display as 0,0,0.
(4)

Veer Singh said:   6 years ago
In C if there is partial initialization then all remaining value will be zero by default. if there is no initialization then garbage value will be by default.

int a[5]={2,3} a[2]=0,a[3]=0.....

if int an [5] only then a[0] a[1] will have a garbage value.
(1)

Ekta_Singh said:   1 decade ago
Default initial value An unpredictable value, which is often.

Called a garbage value for Automatic Storage Class.

Static Storage Class Default initial value - Zero.

And by default when nothing is specify in variable declaration it is auto.

Munivara achari said:   1 decade ago
It will show garbage values if we don not initialize the array size.

int main()
{
int a[] = {2, 3};
printf("%d, %d, %d\n", a[2], a[3], a[4]);
return 0;
}

Now it will print the garbage values.


Post your comments here:

Your comments will be displayed after verification.