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 4 of 4.
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}
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.
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?
Bala said:
1 decade ago
Why the remaining values a[2], a[3], a[4] not take garbage value?
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 ?
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.
Srav said:
1 decade ago
What is partially initialization ?
Priyanka said:
1 decade ago
How to know that an array is patially initialised or not?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers