#include<stdio.h>
int main()
{
int i=2,j;
j = ++i + i;
printf("%d",j);
}
output is 6. try on code compile & run.
yes but what is the reason plz… explain
yes,even i am getting output as 6… but how?.. can anyone pls explain
this is because prefix operator first change the value then assigns or store the value, so ++i first increment and make i=3 after that value is stored in i.
So ++i + i = 3(after incrementing) + 3(as now 3 is stored in i) therefore output = 6.
try running this code
int main()
{
int i=2,j;
//j = ++i + i;
printf("%d\n",++i);
printf("%d",i);
return 0;
}
it will clear your doubt. Output for this will be-
3
3
then plz try this…
#include<stdio.h>
int main()
{
int i=1,j;
j=i++ + i;
}
then output should come 3 bcz 1+2(as two stored in i)
but it comes 2 why??
try this… the output is 3 only as you said 1+2
int main()
{
int i=1,j;
j = i++;
//j = i++ + i;
printf("%d\n",j); // j = 1
printf("%d\n",i); // i = 2
return 0;
}
i use code blocks and it is giving the output 2… thnks for your answer
you are welcome
All of you are getting different outputs because of a single reason, Undefined Behavior. The code invokes Undefined Behavior. Code’s such as
i = i++;
and
j = ++i + i;
or other ones in which the same variable is changed more than once in the same statement leads to Undefined Behavior. Now you may ask what is Undefined Behavior. Refer the link above .
Undefined Behavior can not be predicted. It depends on how your compiler does it. Each person might get different outputs on different compilers because of the way their compiler tries to work this out. The working of these statements using ++
are not defined in the Standard, and hence , the compiler just tries something to get it to work, and each compiler does it differently.
So in short, you cannot predict the answer to that program, it will give different outputs on different compilers. That part is proved seeing the different outputs that different people have posted for this question.
Don’t be surprised if someone gets the output as 165
or even -5487654
, because that’s what Undefined Behavior is capable of. ( In case you’re wondering if there’s anything special about those numbers I gave you, there isn’t, I just typed some random digits )
Also, if you had warnings enabled on your IDE, then it would have detected it, like mine did. I got this warning message
[Warning] operation on 'i' may be undefined [-Wsequence-point]
You can use this as reference
Why are these constructs (using ++) undefined behavior?. And This might also be helpful.
Just in case you’re wondering, I got output as 6 ( but mine was a c++ compiler ).
Now, I will tell you how my compiler made this code work. The ++
operator has higher precedence than the =
and +
operator. You can use this as reference for that.
Now, due to it having higher precedence, firstly, the ++
is evaluated. Since you are using Pre-increment operator, the value of i
, which was initially 2
becomes 3
. Then, next in precedence is the +
operator, and hence, the expression is calculated, which becomes 3 + 3
, and the result is stored in j
, and due to that, the output I obtained was 6
.
But, as I said earlier, the working of this is not Defined in the C Standard, and hence, It depends on how the compiler tries to implement it. My compiler implemented it as I said above, but there is no guarantee that other compilers will do the same, and hence, you might get a different output on another compiler.
Hope you understand that now.
Also, you need to post this as a code. Take EDIT, select your entire code and press Ctrl + k ( assuming you’re on windows and not on Mac )
The output of the above program is 5.
Pre-Increment operator has higher precedence, i is incremented to 3 and addition takes place after that.
3+3=6