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 4 of 4.

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.

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?

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.

Achal said:   1 decade ago
If we take array asa[5]={2,3,3} then ans becomes 3,0,0. how this possible?

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?

Harry said:   1 decade ago
@Vidya Because you use only 3 %d in the printf line.

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];
}

Uday said:   1 decade ago
Here in the program partially initialized are a[2], a[3], a[4]. So they are initialized to 0.


Post your comments here:

Your comments will be displayed after verification.