A question about the condition of using auto-increment and auto-subtraction in parentheses when a while loop is used

Source: Internet
Author: User

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. 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.