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!!
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
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
Any idea which complier among Dev C++, Turbo C is designed according to ANSI standards?
Neither, they have added lots of their own features. GCC is in accordance with the standard. I checked the answer on various compilers and got 16 as answer, but in turbo c, the answer was 15. Sorry for my wrong answer.
Yes, It is 15 in turbo c
@dragonemperor even in C# it is 15 but I am still not getting how could ++x + ++x is equal to 10? which compiler you are using?
gcc compiler
29
6 (Converted in 6 from 5) + 6 (still 6 but now converted in 7) + 8 (converted 7 to 8) + 9 (converted 8 to 9)