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.

Rupinderjit Singh said:   1 decade ago
@Bhavya:

#4 Line:i=++a[1]; //since, a[1](here 1 is an index) points to second position in array element,that is,1(here 1 is element pointed to by index),so i will assign value 1(i=1),but its pre-increment(++a[1]=++1=2) so will be i=2(as simple as it is).Now the same change will also occur in array at index position 1,that is,at a[1],so a[1]=2 also.

#5 Line:j=a[1]++; //Again here also a[1] points to second position in array element,which is now changed to 2 in #4 Line.And just because its post increment additive change will occur in next line.so j will assign value pointed to by a[1],which is 2(Again as simple as it it).

#6 Line: m=a[i++]; //Now look it as m=a[2++](from #4 Line: because i=2 there).Since, its post increment again as it was in #4 Line:.so here m is assign value pointed to by a[2], that is ,third element of an array ,that is, 15.

#7 Line: Now concentrate here,at #6 Line m=a[i++] was post incremented,which mean additive change will occur at next line,and here next line is #7(print("%d, %d, %d",i,j,m);),so here change will occur and i will be 3(2++;from previous line).

SO the out put is 3(#7 Line:), 2(#5 Line:), 15(#6 Line:)

Hope you will get it.

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)

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

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.

Pradyumna said:   1 decade ago
In 1st line the array is initilize as a[0]=5,a[1]=1,a[2]=15,a[3]=20, a[4]=25

In 3rd line i = ++a[1] that means the value of a[1] = 1 is incrmented and sotred in i i.e. i=2

In line 4th j = a[1]++ that means the value of j = 2 bcoz it is post incremnt so j=2

In line 5th m = a[i++] i.e. m = a[2++] i.e. m = a[2] i.e. m = 15 but i becomes 3 bcoz of i++

So the final result is 3,2,15

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)

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)

Rosh said:   5 years ago
@Mohanapriya @Rupinderjit Singh

It was quit understandable explanation but still, I have a doubt j and m both are using post increment then why only m is incremented (when we reach to print statement ). Why not j?

Please explain about it.

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;
}

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


Post your comments here:

Your comments will be displayed after verification.