Simple C Code

answer will be 15.

the answer is 10 As we know that ihis is the case of preincement so in ++a + ++a the value will be first inc. then assign

output will be 9…as the expression is (++x + ++x) so when the first operand is fetched then x is first incremented to 4 and then read and then again when for the next x it is again incremented to 5 and fetched…and finally simply added.

If one more ++x is added to the equation it would be 15…as the next ++x would be fetched as 6.

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

Your program exhibits undefined behaviour so it may give 6 or 7 or 8 or something else as output. Best advice, never do this. http://discuss.codechef.com/questions/41774/post-increment-variable-i-and-assigned-it-to-the-same-variable-i-i?page=1#41822

1 Like

This is UB, check Your program exhibits undefined behaviour so it may give 6 or 7 or 8 or something else as output. Best advice, never do this. http://discuss.codechef.com/questions/41774/post-increment-variable-i-and-assigned-it-to-the-same-variable-i-i?page=1#41822

1 Like

The code is UB. Check Your program exhibits undefined behaviour so it may give 6 or 7 or 8 or something else as output. Best advice, never do this. http://discuss.codechef.com/questions/41774/post-increment-variable-i-and-assigned-it-to-the-same-variable-i-i?page=1#41822

Code is UB. check Your program exhibits undefined behaviour so it may give 6 or 7 or 8 or something else as output. Best advice, never do this. http://discuss.codechef.com/questions/41774/post-increment-variable-i-and-assigned-it-to-the-same-variable-i-i?page=1#41822

Code exhibits UB.

code has UB. Check Your program exhibits undefined behaviour so it may give 6 or 7 or 8 or something else as output. Best advice, never do this. http://discuss.codechef.com/questions/41774/post-increment-variable-i-and-assigned-it-to-the-same-variable-i-i?page=1#41822

Code exhibits UB. Check Your program exhibits undefined behaviour so it may give 6 or 7 or 8 or something else as output. Best advice, never do this. http://discuss.codechef.com/questions/41774/post-increment-variable-i-and-assigned-it-to-the-same-variable-i-i?page=1#41822

Code has UB. Check Your program exhibits undefined behaviour so it may give 6 or 7 or 8 or something else as output. Best advice, never do this. http://discuss.codechef.com/questions/41774/post-increment-variable-i-and-assigned-it-to-the-same-variable-i-i?page=1#41822

No, the code has UB. Check Your program exhibits undefined behaviour so it may give 6 or 7 or 8 or something else as output. Best advice, never do this. http://discuss.codechef.com/questions/41774/post-increment-variable-i-and-assigned-it-to-the-same-variable-i-i?page=1#41822

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.

it will not run on the modern compiler, bt will runinthe older ones.
When printf("%d",++x + ++x); is executed , ++x + ++x will return the value 10(5+5). bt if one more ++x is added to it(++x + ++x + ++x), it will return 18.
Bt if after evaluting the printf ++x + ++x and the adding ++x will give 15.

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.

Ans is 15 man…because for first prefix the x value is incremented an in the meanwhile for next prefix the x value is again incremented…and so for
first ++x:4
Second ++x:5
++x + ++x=9
and if ++x + ++x + ++x=15

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

Answer is 9 because of prefix operatot.First it will increment the value of x to 4 then print.then current value of x is 4 again it get incremented by 1…so 4+5=9.