A question about the condition of using auto-increment and auto-subtraction in parentheses when a while loop is used
A question in Chapter 5 of C Primer Plus:
/* Test program 2015.12.26 */# include
# Define TEN 10int main (void) {int n = 0; while (n ++ <TEN) printf ("% 5d", n); printf ("\ n "); return 0 ;}
At first, I think the output result is 0 1 2 3 4 5 6 7 8 9 10, however, after verification in VC ++ 6.0, the result is 1 2 3 4 5 6 7 8 9 10.
In chapter 5 (P104) of C Primer Plus:
Sequence points help clarify when a suffix increment action occurs. For example, consider the following code:
While (guests ++ <10) printf ("% d \ n", guests );
Sometimes C beginners imagine that "use this value first and then add its value" in this program means that the value of guests is added after the printf () statement is used. However, because guests ++ <10 is the judgment condition of the while loop, it is a complete expression, and the end of this expression is a sequence point. Therefore, C ensures that side effects (increasing the value of guests) occur before the program enters printf. At the same time, the suffix is used to ensure that guests is added after comparison with 10.