0

stumbled upon such a puzzle:

What will be shown on the screen?

#include <stdio.h>

void main()
{
    int x = 10;
    printf("x = %d, y = %d", x--, x++);
}

Curiously enough, but shown at the screen this: x = 11, y = 10; But how??

ks1322
  • 33,961
  • 14
  • 109
  • 164
invo
  • 15
  • 1
  • 3
    Does this answer your question? [Why are these constructs using pre and post-increment undefined behavior?](https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior) – UnholySheep Jun 10 '20 at 17:01
  • 1
    Also: [Parameter evaluation order before a function calling in C](//stackoverflow.com/a/376333) – 001 Jun 10 '20 at 17:01

1 Answers1

1

Argument Evaluation Order is undefined in both C and C++. It's important to avoid any code that passes expressions dependent on each other that has to be evaluated before the function is called. It's a strict no.

int f1() { printf("F1") ; return 1;}
int f2() { printf("F2" ) ; return 1;}
printf("%d%d", f1(), f2()) ;

You can check out by adding several functions that contain a print statement and pass it to a function to observe this in action. You don't know what's coming, the C standard doesn't specify it, it depends on what compiler you use and how it optimizes your code.

Amal K
  • 4,359
  • 2
  • 22
  • 44