ExamVeda
Login
Home
This question belongs to C Program Arrays And Strings
Arrays and Strings
?

What will be the output of the program?
#include<stdio.h>
void 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);
}

Answer & Solution
Correct Answer: Option A

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

>> int i, j, m; The variable i, j, m are declared as an integer type.

>> i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2

>> j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.

>> m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)

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

Examveda
Question posted by Examveda
Community

Join the Discussion

12 Comments
Anees Hafza
Anees Hafza 5 years ago
Good explanation, thank you
Sandeep Kumar
Sandeep Kumar 6 years ago
option no B should be correct please look into seriously
MAHITOSH GIRI
MAHITOSH GIRI 7 years ago
This answer will be b.
Inamul Haque
Inamul Haque 7 years ago
Perect explanation
Anvesh Pandey
Anvesh Pandey 7 years ago
Sir j = a[1]++ so first the value will be stored i think then the incrementation will be done so j = 2 will right
Atharv Jangam
Atharv Jangam 9 years ago
The ans is b
Dipu Saha
Dipu Saha 9 years ago
Hw cm a[1] =2 ? as till i=2 i.e. in the first step..
Can u explain the reaaon for that in detail...
Nehal Kalal
Nehal Kalal 9 years ago
The value of i is supposed to be 2 right?
Examveda
Examveda 10 years ago
Hello Pradeep
Please check the sixth line of explanation for a doubt you have in mind.
Georgian Pradeep
Georgian Pradeep 10 years ago
the value of i=2 as written by you but it varies in option.also the value of m would be a[3]=20 not 15.
Examveda
Examveda 10 years ago
Hello Pradyumna,
Please read explanation clearly.
Pradyumna Bhegade
Pradyumna Bhegade 10 years ago
sir the value of a[1] will be 1 on i as well as on j so the ans will be 2, 1, 15