0

When I execute this code:

#include <stdio.h>
int main(){
int x = 10;
printf("%d  %d  %d  %d\n", ++x, ++x, ++x, ++x);
x = 10;
printf("%d  %d  %d  %d\n", x++, x++, x++, x++);
x = 10;
printf("%d  %d  %d  %d\n",++x, x++, ++x, x++);
}

The result is

14  14  14  14
13  12  11  10
14  12  14  10

I don't understand the sequence of operation precedence in the three printf statements.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
Ahmad Kandil
  • 35
  • 1
  • 7
  • Hello, so the precedence is not only not defined, the result is not defined at all. See the question https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior for explanation what it means. – KamilCuk Mar 07 '22 at 06:59
  • This is simply undefined behavior, see [Why can't we mix increment operators like i++ with other operators?](https://software.codidact.com/posts/278384). But also it is not at all related to operator precedence, but to order of evaluation: [What is the difference between operator precedence and order of evaluation?](https://software.codidact.com/posts/278172) – Lundin Mar 07 '22 at 07:35
  • @KamilCuk Operator precedence is perfectly well-defined and not at all related to this problem. See the 2nd link in my comment above. – Lundin Mar 07 '22 at 07:36

0 Answers0