简体   繁体   中英

About printf function and postfix increment operator (i++) in while loop. I want to know about procedure of the printf function

I want to understand about the procedure of 'printf' function with postfix increment operator.
I debuged these codes and found something that each 'printf' function actives after the end of the while loop.

I expected the result is like these at the second while loop
0 x 0 = 0
1 x 1 = 1
2 x 2 = 4
3 x 3 = 9
but it was wrong

I want to know about flow of the arguments and why the result printed out like this. ;(
Sorry for my poor English and I hope you guys help me solve this problem. thank you.

#include<stdio.h>
int main(void)
{
    int num1 = 0, num2 = 0;

    //test while and postfix increment operator

    //first while
    while (num1 < 30)
    {
        printf("%d x %d = %d\n", num1++, num2++, num1 * num2);
        //the results
        //0 x 0 = 1
        //1 x 1 = 4
        //2 x 2 = 9
        //3 x 3 = 16 ...
        //the procedure of the printf function is from left to right?
        //the flow of arguments is from left to right
    }


    //reset
    num1 = 0, num2 = 0;
    printf("\n");


    //second while
    while(num1 < 30)
    {
        printf("%d x %d = %d\n", num1, num2, (num1++) * (num2++));
        //the results
        //1 x 1 = 0
        //2 x 2 = 1
        //3 x 3 = 4
        //4 x 4 = 9...
        //the procedure of the printf function is from right to left?
        //the flow of arguments is from right to left
        //...why..?
    }

    return 0;
}

There are several duplicates of this question; you're running into the less-obvious form of the problem.

The issue is this:

6.5 Expressions
...
2 If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object , the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings. 84)
C 2011 Online Draft

In the printf call, the expressions num1++ and num2++ have side effects - they change the values stored in those variables. However, you're also attempting to use those variables in a value computation ( num1 * num2 ) without an intervening sequence point - a point in the program's execution where the side effects of ++ have been applied to both num1 and num2 . C does not require that function arguments be evaluated left to right, nor does it require that the side effect of the ++ operator be applied immediately after evaluation.

The behavior is explicitly called out as being undefined - neither the compiler nor runtime environment are required to handle the situation in any particular way.

To accomplish what you want, you need to separate out the updates to num1 and num2 :

 while ( num1 < 30 ) { printf( "%dx %d = %d\\n", num1, num2, num1 * num2 ); num1++; num2++; }

Alternately, you can rewrite this as a for loop as follows:

 for ( num1 = 0, num2 = 0; num1 < 30; num1++, num2++ ) printf( "%dx %d = %d\\n", num1, num2, num1 * num2 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM