cpost-incrementlanguage-concepts

Why does the value of i not increment for i=i++; statement?


Code:

for(int i=0;i<5;){
  i=i++;
  printf("%d",i);
}

The above program print zeros infinitely, How is that possible? There is the statement i=i++;. Please explain why the value of i do not increment.


Solution

  • The statement i = i++ is undefined behaviour in C. Simplistically, modifying and using the same object without an intervening sequence point is not guaranteed to work in any way you expect.

    Sequence points are covered in Appendix C of the ISO C standard if you're interested in an in-depth investigation. Basically, they consist of: