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 3 of 4.
Prem chand said:
1 decade ago
What is the output for this:
main()
{
int a[2]={1,2,3};
printf("%d,a[3],a[4];
}
main()
{
int a[2]={1,2,3};
printf("%d,a[3],a[4];
}
Harry said:
1 decade ago
@Vidya Because you use only 3 %d in the printf line.
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?
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?
Achal said:
1 decade ago
If we take array asa[5]={2,3,3} then ans becomes 3,0,0. how this possible?
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.
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.
Sam said:
1 decade ago
It will show garbage values if we do not partially initialize the array.
And referring c programming language by dennis ritchie, it says that only extern and static variables are initialized as zero by default?
And referring c programming language by dennis ritchie, it says that only extern and static variables are initialized as zero by default?
SUNIL(THE PAIN) said:
1 decade ago
Why is not showing the garbage value, if we do not partially initialize the array?
I didn't get from the above discussion.
I didn't get from the above discussion.
Ravi said:
1 decade ago
C does not contain garbage collector so remaining elements will be garbage.
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?
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers