Simple C Code

29 is the answer for sure …as
(5+1)=6+6+8(inc the stored value 7)+9(inc 8 by 1)=29

Many answers are posted for this quesion and all are wrong in the same way.

something = ++x + ++x;  /* This assignment takes place internally while calling function */

Above line of code exhibits undefined behavior. For an LOC(line of code) with undefined behavior, you can not question why, how etc. (Output differs with compiler, differs with time etc)

LOC of interest exhibits undefined behavior because variable i is being updated twice before next sequence point is reached.

This faq is worth a read for you.

C99 standard

  • 6.5 Expressions, §2

Between the previous and next sequence
point an object shall have its stored
value modified at most once by the
evaluation of an expression.
Furthermore, the prior value shall be
read only to determine the value to be
stored.

  • 6.5.16 Assignment operators, §4:

The order of evaluation of the
operands is unspecified. If an attempt
is made to modify the result of an
assignment operator or to access it
after the next sequence point, the
behavior is undefined.

p.s.
Any LOC in C language can show one of 5 behaviors given below:

  1. Well defined: For ex value of i
    after i = 2 + 3 * 5;
  2. Implementation dependent: For ex value of sz in size_t sz = sizeof (int);
  3. Unspecified: For ex: Order of evaluation in, add(print(“hi”), print(“Hello”));
  4. Undefined: For ex: value of i afer i = 0; i = i++; :slight_smile: [it can be 0 or 1 or 100 or -500]
  5. Locale specific: For example what would be printed when you do printf(“\\”);

If you still find something unclear, please ask.

the answer is 9.
because (++/operator) is added before the variable which means it increment the value first the store to give a o/p.

In case of turbo compiler answer wil be 15

firstly ++x increase value of x by 1
then add all x;
their is increament in x every time so last value of x get added

  1. ++x --> 4
  2. ++x --> 5
    now value of x is 5 ‘++x + ++x’ is 10
    and if their is again a ++x
    then
  3. ++x --> 6
    now ans is 15

ans will be 10 as preincrement follows first change then use

++x means first increment if x=3 then ++3 is 4.
++x + ++x=4+5=9

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

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