C Programming - Arrays - Discussion
Discussion Forum : Arrays - Find Output of Program (Q.No. 1)
1.
What will be the output of the program ?
#include<stdio.h>
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d, %d, %d", i, j, m);
return 0;
}
Answer: Option
Explanation:
Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is initialized to
a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 .
Step 2: int i, j, m; The variable i,j,m are declared as an integer type.
Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2
Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.
Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)
Step 6: printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m
Hence the output of the program is 3, 2, 15
Discussion:
40 comments Page 3 of 4.
Dev said:
7 years ago
Thanks @Gayathri.
Anita said:
8 years ago
Please explain it to me to understand the concept of this.
Yogita sable said:
8 years ago
I don't understand please can you explain it again?
Mayank said:
8 years ago
Hi,
++a[1] can we written as ++ *(a+1); (associativity right to left)
as *(a+1)=1;
++1; should give error.
But it works why?
++a[1] can we written as ++ *(a+1); (associativity right to left)
as *(a+1)=1;
++1; should give error.
But it works why?
Venkatesh said:
9 years ago
int a[5]={75,94,123,566,334};
instead of that given array use this.
instead of that given array use this.
Raghu said:
1 decade ago
I can't undrstand at 5th line in this explanation.
Please any one can explain this.
Please any one can explain this.
OMNARAYAN TIWARI said:
1 decade ago
The value of m=a[i++] not clear and its output is also confusing. It seems it's giving the output of j, i, m.
Priya said:
1 decade ago
The order of printf function is i, j, k. Then it o/p is 2 3 15.
Selena said:
1 decade ago
In some function call,the order of passing arguments become important.
C's calling convention is from right to left.
So, first of all m is executed as it is first from right to left.
m = a[i++];
Here compiler don't know the value of i,so it would go to i.
i = ++a[1] becomes ++1.Hence i = 2 and a[1] =2.
Now, m = a[i++] becomes m = a[2++]. Hence m = a[2] = 15 and i is incremented by 1, i.e. i becomes 3.
j= a[1]++ becomes j = 2++. Hence j = 2 and a[1] = 3.
However,they will be displayed in the order they are written in the printf() function.
C's calling convention is from right to left.
So, first of all m is executed as it is first from right to left.
m = a[i++];
Here compiler don't know the value of i,so it would go to i.
i = ++a[1] becomes ++1.Hence i = 2 and a[1] =2.
Now, m = a[i++] becomes m = a[2++]. Hence m = a[2] = 15 and i is incremented by 1, i.e. i becomes 3.
j= a[1]++ becomes j = 2++. Hence j = 2 and a[1] = 3.
However,they will be displayed in the order they are written in the printf() function.
Manikandan said:
1 decade ago
This program output is confused. Please tell how the output print like this.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers