Calculation sequence of printf cout Parameters

Source: Internet
Author: User

1. The execution sequence of printf () parameters is from right to left!

Why? I think printf () uses the data structure stack. the stack is first-in-first-out, and then output in the desired order. Then, the implementation of printf may be to calculate the parameters first, then go to the stack. The stack is displayed in the output and the expected result is displayed! (But I don't know exactly what it is, but it is similar to the cout output principle ).


#include <stdio.h>int f1(){printf("f1()\n");return 1;}int f2(){printf("f2()\n");return 2;}int f3(){printf("f3()\n");return 3;}void main(){printf("%d,%d,%d\n",f1(),f2(),f3());}

Output result:

////////////// // Reprinted from the

In addition, the following procedures are explained:
Int I = 7; printf ("% D \ n", I ++ * I ++ );
Output result: 49 instead of 56
Int I = 7; printf ("% d \ n", ++ I, I ++ );
The output result is: 8 7 instead of 9, 7. This does not violate the reason for splitting the printf function from the right to the left.

The reason for this result is:
Although the suffix auto-increment and suffix auto-subtraction operators ++ and--The operation is performed only after the old value is output. However, after ''is often misunderstood. There is no guarantee that auto-increment or auto-increment will be performed immediately after the original value of the output variable and before the rest of the expression is calculated. It cannot be guaranteed that the variable update will be completed in the '''complete' expression. (According to the ansi c term,
Before the next ''sequence point", see Question 3.7. In this example, the compiler chooses to multiply the old value of the variable before auto-incrementing the two.

The behavior of code containing multiple uncertain side effects is always considered undefined. (To put it simply, ''multiple uncertain side effects "refer to the auto-increment that causes the same object to be modified twice or referenced after being modified in the same expression, any combination of subtraction and value assignment operators. This is a rough definition. For a strict definition, see Question 3.7,
For the description of ''undefined ", see question 11.32 .) Do not even try to explore how these things are implemented in your Compiler (this is the opposite of the mental exercises in many c textbooks); just as K & R
It is wise to point out, ''if you don't know how they are implemented on different machines, such ignorance may help to protect you. Begintex2html_deferred

4.7 how can I understand complex expressions? ''What is a sequence point?
A sequence point is a time point (after the entire expression is fully calculated or|,&&,? : Or a comma operator, or before a function call), the dust is settled at this moment, and all side effects are ensured to end. The ANSI/iso c standard is described as follows:

The value saved by an object between the previous and next sequence points can only be modified once by the calculation of the expression. The previous value can only be used to determine the value to be saved.

The second sentence is confusing. It means that if an object needs to be written in an expression, access to the object in the same expression should be limited to directly used to calculate the value to be written. This rule effectively limits the validity of expressions that can only access variables before modification. For example, I = I + 1 is valid, while a [I] = I ++ is invalid (see Question 3.1 ).

See question 3.8 below.

4.1 why is this code: A [I] = I ++; not working? The subexpression I ++ has a side effect-it will change the value of I-because I is referenced elsewhere in the same expression, this will lead to undefined results, there is no way to determine whether the reference (in a [I] on the left) is an old value or a new value. (Note: although the behavior of such expressions is not certain in K & R, the c Standard strongly states that it is undefined. See question 11.32.

//////////////////////////////////////// //////////////////////////////////////// ///////////////////////////

2. cout output sequence

Or the above program:

#include <iostream>using namespace std;int f1(){cout<< "f1()" << endl;return 1;}int f2(){cout<< "f2()" << endl;return 2;}int f3(){cout<< "f3()" << endl;return 3;}void main(){cout<<f1()<<f2()<<f3()<<endl; }


It can be seen that cout is similar to printf in principle:


1. The cout and printf parameters are in the stack order from right to left.

2. The cout and printf parameters are calculated from right to left.

3. But it is better not to call the function in the output. This is simple, but different compilers may have different results!

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: 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.