According to me knowledge with pre and post increment in C. A post-increment
will take effect after the statement.
Example:
int num = 10;
int sum = num ++; //sum & num at this point is 10
printf("%d %d", sum, num); //we should get 10 11
While pre-increment
takes effect immediately.
int num = 10;
int sum = ++ num ; //num is evaluated to 11 first, then pass in to sum
printf("%d %d", sum, num); //we should get 11 11
The following statements will generate unexpected results due to undefined behaviour in C.
int x = 5;
printf("%d %d %d %d\n", ++x, ++x, ++x, x);
int y = 5;
printf("%d %d %d %d\n", y++, y++, y++, y);
Program Output in C:
8 7 6 5
7 6 5 5
My question is: Why can't C simply determine the sequence point from left to right? All my past lecturers in school taught us that in programming, it is evaluated from left to right. But why in this specific case, the C standard finds it so difficult to determine the order of execution. I am more curious why can't they interpret it from left to right? What is reason they can't do so?
PS: I've visited many posts of similar title. But non address this issue: Why are these constructs (using ++) undefined behavior?