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;
}
Answer: Option
Explanation:
When an automatic array is partially initialized, the remaining elements are initialized to 0.
Discussion:
38 comments Page 1 of 4.
Priyanka said:
1 decade ago
How to know that an array is patially initialised or not?
Srav said:
1 decade ago
What is partially initialization ?
Krishna said:
1 decade ago
Partially initialize means we define some values and some are not defined. If we not define it is initialize to 0.
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 ?
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 ?
Bala said:
1 decade ago
Why the remaining values a[2], a[3], a[4] not take garbage value?
Jyoti said:
1 decade ago
By default array are auto class variable so it should be print garbage value why it is printing 0, 0, 0 don't know?
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.
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.
Penchal said:
1 decade ago
The arry function automatically takes the free values as '0'
ex:-
arr[4]={1,2};
It will store the arry as arry[4]={1,2,0,0}
ex:-
arr[4]={1,2};
It will store the arry as arry[4]={1,2,0,0}
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.
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.
Ananya said:
1 decade ago
@Raghav.
But When no initialization is given then also it is showing the output as zero. But as you said it should print some garbage value. Then why it is happening?
But When no initialization is given then also it is showing the output as zero. But as you said it should print some garbage value. Then why it is happening?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers