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

Shanme said:   9 years ago
When we declare any array function, it will assign it s value start from a[0], a[1],...................

Eg:
int a[4]={1,2,3,4,5};
then
a[0]=1,a[1]=2, ........a[4]=5.

Jayakumar said:   9 years ago
Every index value is Start is 0.

So, int a[5]={2,3}
a[0]={2}
a[1]={3}
a[2]={0}

Automatically initialized the value is called zero.
So answer is called 000.

Rish said:   9 years ago
Thanks @Raghav.

Sagar said:   9 years ago
@ALL.

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

Sonam said:   8 years ago
Please explain, how can be solve this?

Ravali said:   1 decade ago
What is an automatic array?

Sahil said:   7 years ago
I think it should have garbage value. Right?

Saikumar Reddy said:   7 years ago
Give me an another example for this answer.

Rohit said:   2 years ago
Ideally, it depends on the compiler to generate code to initialize a partially initialized array or not. Sometimes to save time it is not necessary to initialize it to zero.

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


Post your comments here:

Your comments will be displayed after verification.