1
 main(){

    int x = 256, y = 4;
    printf("%d\n\n", x++ + ++y);   //output = 261
    printf("%d\n\n", x);          // output = 257
    printf("%d", y);              // output = 5

}

Is the final answer 261, because 256 -> 257 (post operator) and 5 -> 5 (pre operator) cause 256 + 5 = 261?

ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42
Cascara
  • 83
  • 1
  • 3
  • it is all a matter of when the variable is printed. if preincrement, the variable is printed after the increment. if postincrement, the variable is printed before the increment. – user3629249 Aug 28 '15 at 23:51
  • `main()` is incorrect. Use [`int main(void)`](http://stackoverflow.com/q/204476/995714) instead – phuclv Aug 29 '15 at 02:33

2 Answers2

4

Given:

int x = 256, y = 4;
printf("%d\n\n", x++ + ++y);

In short: The x++ returns the value 256 and then increments x to 257. The ++y increments y to 5 and returns the value 5. The addition, therefore, adds 256 and 5 yielding 261.

Long windedly: The x++ evaluates to the current value of x which is 256 and schedules an increment of x to 257. Similarly ++y schedules the increment of y to 5 and evaluates to the incremented value 5. The addition, therefore, adds 256 and 5 yielding 261. The order in which the terms involving x and y are evaluated is not defined, but both have to be evaluated before the addition (though the increments may not be complete when the addition is evaluated). Because there is a 'sequence point' when the arguments (and the expression denoting the function) have been evaluated but before the function is called, the increments must be complete when printf() is called.

The next two statements print x and y as 257 and 5.

Note that those two printf() operations could be combined into one. Neither could be combined with the first without invoking undefined behaviour. (See Multiple increments and undefined behaviour for more information on this topic.)

So, allowing for the fact that I would not express it quite the way you wrote it, you seem to have the correct explanation.

Also, Standard C has required a return type on all functions for over 15 years now (since C99 was standardized). You should write:

int main(void)

for a main() function that takes no arguments. (See What should main() return in C and C++? for the full details.)

Note that this question only invokes fully defined behaviour (at least, in the printf() statements). It is not asking about multiple increments on a single variable between sequence points.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Nit: `x++` *evaluates* to `256` and `++y` *evaluates* to `5`, but `x` and `y` may not be updated immediately after evaluation; the only requirement is that they be updated by the next sequence point, which is prior to the function call. This is part of why expressions like `x++ + x++` invoke undefined behavior; the order in which the expressions are evaluated *and* the order in which the side effects are applied is unspecified, so the behavior isn't consistent. – John Bode Aug 29 '15 at 01:24
1

It will show UNSPECIFIED behavior

In your case, we can't tell whether x++ will be evaluated first or ++y will be evaluated first. It is compiler dependent.
So don't use expressions involving a combination of post-increment or pre-increment operators in C or C++.

For more information refer to the link:
https://www.quora.com/What-does-an-expression-involving-multiple-post-pre-decrement-increment-operators-evaluate-to-in-C-and-C++#

Ashish kumar
  • 183
  • 1
  • 4
  • 10
  • 2
    Actually it is not UB. While it might be undefined if x++ or ++y will be evaluated first, they do not depend on each other, so the final result is well defined – user1781290 Jul 19 '19 at 16:47
  • From undefined behavior, I mean we can't tell the exact answer and it is compiler dependent. Refer to the example given in the link: https://stackoverflow.com/a/4176333/7873716 – Ashish kumar Jul 20 '19 at 17:22
  • Check the answer you linked. "it is unspecified whether x++ or y++ will be evaluated first." and "etween the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression." are the key points here. It is safe to modify x++ and y++, but UB to use x++ and ++x without sequence points – user1781290 Jul 20 '19 at 21:26
  • We can't tell if x++ or ++y are evaluated first, that is correct, but for the question this is actually irrelevant, as both cases lead to the same result – user1781290 Jul 20 '19 at 21:29