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

Yogita sable said:   7 years ago
I don't understand please can you explain it again?

Bheemesh said:   1 decade ago
Out put cannot understand clearly. Explain clearly.

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

Sonu said:   1 decade ago
I think Pradyumna is correct. thanks dude..

Deepak said:   1 decade ago
I don't understand this concept.

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

Anandesh said:   8 months ago
2,3,20 is the answer.

Manasa said:   9 years ago
Why i value is 3?

Dev said:   7 years ago
Thanks @Gayathri.


Post your comments here:

Your comments will be displayed after verification.