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

Sri hari said:   10 years ago
In a array always the numbering of elements start from 0 or 1? We need to consider the starting element as a[0] or a[1].

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?

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.

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

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

Ravi said:   1 decade ago
C does not contain garbage collector so remaining elements will be garbage.

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

Bala said:   1 decade ago
Why the remaining values a[2], a[3], a[4] not take garbage value?

Sagar said:   9 years ago
@ALL.

The array of a [4] means;
Start from a [0] to a [3].

Priyanka said:   1 decade ago
How to know that an array is patially initialised or not?


Post your comments here:

Your comments will be displayed after verification.