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.
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:
&&
; logical OR ||
; comma ,
.?:
operator and whichever of the second and third operands is evaluated.if
or switch
); the controlling expression of a while
or do
statement; each of the (optional)
expressions of a for
statement; the (optional) expression in a return
statement.