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.
Siva said:
1 decade ago
As per gcc compiler it will take garbage values where non existing elements and existing elements a[0] = 2, a[1] = 3, a[3] = garbage value, a[4] = garbage value.
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].
Priya said:
9 years ago
#include<stdio.h>
int main()
{
int a[3] = {2, 3};
printf("%d, %d,%d\n", a[2], a[3],a[4]);
return 0;
}
Output for above program is like this: [0,-1612,-2697].
Why compiler giving garbage value if it is partially initialized?
Please explain and correct me if I am wrong..!
int main()
{
int a[3] = {2, 3};
printf("%d, %d,%d\n", a[2], a[3],a[4]);
return 0;
}
Output for above program is like this: [0,-1612,-2697].
Why compiler giving garbage value if it is partially initialized?
Please explain and correct me if I am wrong..!
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.
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.
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].
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?
Vasu said:
8 years ago
There is a[5] = {2,3,0,0,0}.
i.e. a[0] = 2.
a[1] = 3.
a[2] = 0.
a[3] = 0.
a[4] = 0.
So, the value of a[2]=0, a[3]=0 & a[4]=0.
i.e. a[0] = 2.
a[1] = 3.
a[2] = 0.
a[3] = 0.
a[4] = 0.
So, the value of a[2]=0, a[3]=0 & a[4]=0.
(1)
Raja Ravi said:
7 years ago
What is automatic array?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers