Simple C Code

Can anyone explain what is the output of the below code and how? What if there is one more ++x?

#include
main()
{
	int x=3;
	printf("%d",++x + ++x);
}

for the given code the output will be 10…if there is one more ++x it will be 16.
in the first case the two operands for addition are the values in x itself.
so ans=2*(value present in x).
now x is incremented 2 times,hence value in x =5.
therefore ans=2(5)=10
if one more ++x is present , then value in x is incremented and then added with 10,now already x has been incremented twice ,so after another increment it will have 6.hence ans=10 +6=16(in the second case).

1 Like

the answer is 10. pre-increment operators are evaluated before evaluation of the actual expression.
Here there are two pre-increment operators which change the value of x by incrementing it twice, as a result, its value changes to five (two increments). Then the addition operation takes place. So the new value of x is added twice (i.e. five plus five) giving an answer of 10.

Had there been three pre-increment operators,like ++x + ++x + ++x, then before the operation, the x would have been incremented three times changing its value to 6 and three additions would give the answer 18.

Another interesting thing to note is post-increment is evaluated after the expression. Consider this expression :
++x + ++x + x++

there are two pre-increment which change the value of x to five. The post increment does not change the value of x. So, there is an addition of three x’s whose value is 5. So the result is 15. Then post-increment takes place making the value of x==6.

code:

x=3;

printf("%d\n",++x + ++x + x++);

printf("%d\n",x);

output is:

15
6                              // x is incremented after the evaluation of the expression

p.s. Try a few more variations and guess the answer before running the code and check your answer with your code.

2 Likes

@ dragonemperor As you said the answer in the second case isn’t 18 but 16. Any explanation will be helpful…thanks!

@ prem_93 From your explanation for the first case the answer for the second one should be 3*6 right!! What if there is one more ++x?

Can you please check the answer in any non-gcc compiler like turbo c++ or dev cpp? I have only gcc and getting 16 as answer. My concept might be wrong.

Shouldn’t the incrementation work before evaluation of the expression making the value of x=6?

Same here. I’ll check though. I presume that doesn’t make any difference right!!

actually the answer will be 9 because first you have x which is equal to the 3 then you have ++x + ++x
lets divide this in three parts “++x” “+” “++x”
(A) (B) ©
now the value of (A) is 4 because you have incremented x (=3) with 1 then (B) is the addition which will add (A) {which is equal to 4} with © now come the value of © you have already incremented the x to 4 now you have the value of x = 4 before step © so after the increment at © it will be 5 so © {equal to 5}
now (A) (B) ©
4 + 5 = 9

1 Like

Some times, different compilers give different answers if compiler is not designed according to ISO/ANSI standards. But this should not be the case in gcc though.

Please run the code in any compiler and check the output.

printf("%d\n",++x + ++x + x++);
This will not print 18 but it prints 16.the explanation is :
ADDITION OPERATOR TAKES IN ONLY TWO INPUTS AT A TIME…
In the first case, there are only two operands and hence ans is 10.

But in the second case,there are 3 operands .now the operation is performed on the first two operands and the answer is stored in a temp register,this value is 10.For the second addition operation value stored in temp register and the new operand are added up.the new operand is ++x(which will be 6)(NOTE:performing ++x will not change temp value(10)) and hence answer is 10 +6=16.

if there is another ++x then this 16 value(from the previous case) is stored in the temp register and the new operand ++x(which will now be’7’)is added with it and hence answer would be 16+7 =23.
if this is followed by another ++x ans would be 23+8=31 and so on.
i think this would help,if you still hve any doubt comment below…if you found my post helpful upvote and mark it as accepted answer…

the answer is 9 in turbo C

2 Likes

what about ++x + ++x + ++x ???

in turbo c
?

++x + ++x + ++x in turbo C is 15 as expected ( 4 + 5 + 6)
and further more will be like this 4+5+6+7+8+9+10…

even in C# ++x + ++x is 9 in Visual Studio express

1 Like

@geeksoul Suprising! Thanks for the info that was helpful.

1 Like

Any idea which complier among Dev C++, Turbo C is designed according to ANSI standards?