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:
39 comments Page 1 of 4.
Manjuladevi said:
1 decade ago
This program output is confused. Please tell how the output print like this.
(2)
Saranya said:
1 decade ago
Sorry i can't understand at step4 of this program explanation
j=2 then there should occur a[2],
but a[1] is came how, and
we get the value a[1]=3;
Please explain.
j=2 then there should occur a[2],
but a[1] is came how, and
we get the value a[1]=3;
Please explain.
Bhimeswarrao said:
1 decade ago
At step 4 a[1]=2 so it is directly assigned to j. Then a[1] is incremented by 1.
Because as it is a post increment finaly at step 4 j=2 and a[1]=3.
Because as it is a post increment finaly at step 4 j=2 and a[1]=3.
Bheemesh said:
1 decade ago
Out put cannot understand clearly. Explain clearly.
Gayathri said:
1 decade ago
First understand the logic of post and pre increment.....if ++x means x is incremented by 1 and gets stored in X.
Let
x=1
y=++x and
y=x++
in first case y will be 2 and x will be 2.(that is X is incremented first and then stored in y)
in second case y will be 1 and x will be 2.(that is x is stored in y and the incremented)
try to understand this logic first..........
Similarly,
in the stmt , i = ++a[1];
since a[1]=1, then after execution of the above stmt,
i will be 2,a[1] will be 2
similarly,
in the stmt , j = a[1]++;
since a[2]=2 now(after execution of I stmt,)
j ll be 2 (post increment will happen) and a[2] will be 3 now..
in the 5th case,
m = a[i++], i is post incremented... so a[i] will be a[2] and i will be 3... so m=a[2]=15
Let
x=1
y=++x and
y=x++
in first case y will be 2 and x will be 2.(that is X is incremented first and then stored in y)
in second case y will be 1 and x will be 2.(that is x is stored in y and the incremented)
try to understand this logic first..........
Similarly,
in the stmt , i = ++a[1];
since a[1]=1, then after execution of the above stmt,
i will be 2,a[1] will be 2
similarly,
in the stmt , j = a[1]++;
since a[2]=2 now(after execution of I stmt,)
j ll be 2 (post increment will happen) and a[2] will be 3 now..
in the 5th case,
m = a[i++], i is post incremented... so a[i] will be a[2] and i will be 3... so m=a[2]=15
Mani said:
1 decade ago
i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2
here i=++a[i]; -->>(i=++1 -->>i=2 how a[1]=2)
we are not toughing the array elements we are assining the i j k variables using array elements
here i=++a[i]; -->>(i=++1 -->>i=2 how a[1]=2)
we are not toughing the array elements we are assining the i j k variables using array elements
Murali said:
1 decade ago
For the following code, the output is 2,2. Could anyone please explain?
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int j, m;
j = a[1]++;
m = a[j++];
printf("%d, %d",j, m);
return 0;
}
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int j, m;
j = a[1]++;
m = a[j++];
printf("%d, %d",j, m);
return 0;
}
Biswajit said:
1 decade ago
Hi Murali,
j=a[1]++;
Here, a[1] point to array's second element 1 that assign to j, so j=1; then array's second element increment by 1, so, now a[5]={5,2,15,20,25}.
m=a[j++];
Here, a[j++] point to array's second element 2 that assign to m, so, m=2; and therefore j is incremented, so, now j=2.
j=a[1]++;
Here, a[1] point to array's second element 1 that assign to j, so j=1; then array's second element increment by 1, so, now a[5]={5,2,15,20,25}.
m=a[j++];
Here, a[j++] point to array's second element 2 that assign to m, so, m=2; and therefore j is incremented, so, now j=2.
(1)
Manikandan said:
1 decade ago
This program output is confused. Please tell how the output print like 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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers