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;
}
2, 1, 15
1, 2, 5
3, 2, 15
2, 3, 20
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.

Jaydeep B said:   3 years ago
I think the correct answer would be 2, 3, 15.
(17)

Tharun parsa said:   1 year ago
The answer is 3,2,15.

Because
i=++a[1]=++1 (since a[1]=1) then i=2
i=2,and a[1]=2 (because pre-increment )
j=a[1]++=2++ (since a[1]=2 here)
j=2 and a[1]=3
m=a[i++] that is i=2 and only i value will be taken in the square bracket then it will be incremented
m=a[2++]
which means m=a[2 ]and then i value increases by 1 then i =3

So, m=a[2]=15
Hence the output is 3,2,15.
(6)

R.Amirthavarshini said:   6 years ago
I think 2, 3, 20 is the answer.
(4)

Shantaras said:   3 years ago
Sir,

M=a (2) taken why not a (3). (because I is incremented).
(3)

Vaibhav said:   7 years ago
M=a[i++] if i is 2 after increment i becomes 3 then a[3]=20.

I can't understand, why (i) not incrementing by 1 in 5th step? Please, someone, help me to get this.
(3)

Manjuladevi said:   1 decade ago
This program output is confused. Please tell how the output print like this.
(2)

Mohanapriya.N said:   1 decade ago
Mind in post and prefix increment. Here,

i = ++a[1] --- this pre increment. i.e. the value is increased and assigned in variable (i). a[1] = 1 incremented to 1 hence we get i = 2 now.. (note carefully).

j = a[1]++ --- This is post increment. After assigning the value to the variable (j), a[1] value incremented. Already a[1] increased and assigened in "i". the increased value is 2. That is assiged to "j". (this is difference between prefix and postfix) j = 2.

m = a[i++] -- Already we have "i" value as 2. this is also post incremented increment function is not done. Just value assigned i.e. a[2].
Here a[2] is 15 hence m = 15. THis is not finished.

Post incremented done after assigning the value to the variable. Here the assign the variable to m that is "2" after that it is incremented (2+1 = 3) that value stored in "i". Hence we get the out put i = 3 now only.
(1)

Kaviya.p said:   3 years ago
@Rosh.

The value of j is not incremented the a[1] value is incremented see its because of post incremented.
(1)

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.
(1)

Dev said:   7 years ago
Thanks @Gayathri.


Post your comments here:

Your comments will be displayed after verification.