A small problem in the order of C expressions

Source: Internet
Author: User

Http://sunxiunan.com /? P = 1742

Browse Q. yuyun's blogArticleHttp://www.rainsts.com/article.asp? Id = 959 I found a small problem. It is estimated that some people with similar ideas also have one. So record it.

The problem is that:

"Obviously, according to The cdecl rule," printf (..., The printf function parameters in test (2), test (1) "are sequentially set from right to left (this statement is used for the moment ). Therefore, test (1) is called first, and then test (2). The above assemblyCodeThis is also explained ."

Despite the fact, this is a question.

The cdecl inbound order is correct. The inbound order is for the expression results separated by commas. That is to say, each result must be in this order. However, the order of expression calculation (or the call of each function separated by commas) is not specified. The last section on page 52 of K & R photoprinting plate is very clear. The following F and G may not be called first:

C, like most versions ages, does not specify the order in which the operands of an operator are evaluated. (exceptions are & |? : And ",") for example X = F () + g ();

Start on page 63:

The commas that separate function arguments, variables in declarations, Etc., are not comma operators, and do not guarantee left to right evaluation.

This means that the comma-separated function parameters are different from the comma operator, and the order of calculation from left to right is not guaranteed (or from right to left, of course ).

Note that this evaluation is actually a call to test (2) and test (1. If these calls have boundary effects, different results may be obtained in different compilers and operating systems. Another similar problem is that the same variable is called more than twice in the parameter list.

If you want to ensure the order of value (note that it is not the order of the result), you can only use a temporary variable to save the call results, or use a comma to operate, & | this ensures the order of values.

In fact, I have mentioned this issue in my previous blog, but I can repeat it once again. Http://sunxiunan.com /? P = 1684

References:

Article 7 http://www.andromeda.com/people/ddyer/topten.html reference

C traps and defects 3.7 Value Order

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.