0

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?

Out put of this program .Confused ?

Community
  • 1
  • 1
user3437460
  • 17,253
  • 15
  • 58
  • 106
  • 1
    The question you linked explained it very well. Which part don't you understand? – Yu Hao Nov 23 '14 at 13:12
  • I can comprehend when it is in a normal single line expression. However, I hope someone could explain why the increment is evaluated from the right hand side? – user3437460 Nov 23 '14 at 13:15
  • 1
    Read the duplicate question you linked yourself **carefully**. It's not evaluated from right to left, the order is unspecified. – Yu Hao Nov 23 '14 at 13:18
  • Further more, why Java doesn't look at is as an undefined behaviour yet C does? – user3437460 Nov 23 '14 at 13:19
  • 3
    Because Java is a different language. – a3f Nov 23 '14 at 13:19
  • Java guarantees that expressions are evaluated from left to right, and that the side effects of operators like `++` and `--` are applied immediately upon evaluation. C does not; with a few exceptions, C leaves the order of evaluation *unspecified*, and it does not guarantee that side effects are applied immediately. – John Bode Nov 23 '14 at 13:39
  • @YuHao pls reopen the question, I have edited it. – user3437460 Nov 23 '14 at 13:43
  • @YuHao Can someone help me undo that negative vote? :( – user3437460 Nov 23 '14 at 13:48
  • @user3437460: the pre-increment is not guaranteed to take effect immediately; the result of `++x` is `x+1`, but `x` doesn't have to be modified immediately upon evaluation. I think this is the source of your confusion. – John Bode Nov 23 '14 at 13:52
  • @JohnBode Thanks a lot for your reply. you may consider leave your answer below? Can you also help me neutralise that downvote with an upvote? :) – user3437460 Nov 23 '14 at 13:55
  • There is a difference between the things you "can't do" and the things you "don't do". As you've noted, Java has different semantics. If you want a general exploration into *"why have undefined behavior in cases when you could define it?"*, then that is discussed a number of places... you can search a bit about [philosophy behind undefined behavior](http://programmers.stackexchange.com/questions/99692/philosophy-behind-undefined-behavior) – HostileFork says dont trust SE Nov 23 '14 at 14:05
  • @user3437460: The reason C doesn't specify the order of evaluation is to allow compilers to optimize those evaluations. It was a deliberate design decision. When Gosling designed Java, he decided to trade optimization for predictability. – John Bode Nov 23 '14 at 18:02
  • @JohnBode If only you could place your answer below, I will definitely thumbs up your answer or even accept it. You are the few rare ones willing to explain rather than simply make my post as duplicate which isn't constructive at all. – user3437460 Nov 23 '14 at 18:41
  • @user3437460: the question has been marked as a duplicate; no answers are being accepted. – John Bode Nov 23 '14 at 22:07

0 Answers0