Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 关于while循环中的printf函数和后缀增量运算符(i+;+;)。我想知道printf函数的过程_C_While Loop_Printf_Increment_Operator Keyword - Fatal编程技术网

C 关于while循环中的printf函数和后缀增量运算符(i+;+;)。我想知道printf函数的过程

C 关于while循环中的printf函数和后缀增量运算符(i+;+;)。我想知道printf函数的过程,c,while-loop,printf,increment,operator-keyword,C,While Loop,Printf,Increment,Operator Keyword,我想了解带后缀增量运算符的“printf”函数的过程。 我调试了这些代码,发现每个“printf”函数在while循环结束后都会激活。 我希望第二个while循环的结果是这样的 0 x 0=0 1 x 1=1 2x2=4 3 x 3=9 但这是错误的 我想知道论点的流程,以及为什么结果是这样打印出来的;( 对不起,我的英语很差,希望你们能帮我解决这个问题。谢谢 #include<stdio.h> int main(void) { int num1 = 0, num2 =

我想了解带后缀增量运算符的“printf”函数的过程。
我调试了这些代码,发现每个“printf”函数在while循环结束后都会激活。

我希望第二个while循环的结果是这样的
0 x 0=0
1 x 1=1
2x2=4
3 x 3=9
但这是错误的

我想知道论点的流程,以及为什么结果是这样打印出来的;(
对不起,我的英语很差,希望你们能帮我解决这个问题。谢谢

#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;
}
#包括
内部主(空)
{
int num1=0,num2=0;
//测试while和后缀增量运算符
//先
而(num1<30)
{
printf(“%d x%d=%d\n”,num1++,num2++,num1*num2);
//结果
//0 x 0=1
//1 x 1=4
//2x2=9
//3x3=16。。。
//printf函数的过程是从左到右?
//论证的流程是从左到右
}
//重置
num1=0,num2=0;
printf(“\n”);
//第二时间
而(num1<30)
{
printf(“%d x%d=%d\n”,num1,num2,(num1++)*(num2++);
//结果
//1 x 1=0
//2x2=1
//3x3=4
//4x4=9。。。
//printf函数的过程是从右到左?
//论证的流程是从右到左
//……为什么。。?
}
返回0;
}

这个问题有好几处重复;您遇到的是不太明显的问题

问题是:

6.5表达式

2如果标量对象上的副作用相对于其他副作用未排序 在同一标量对象上或使用同一标量的值进行值计算 对象,则行为未定义。如果 表达式的子表达式,如果这样一个未排序的边 在任何订单中都会产生影响。84) 在
printf
调用中,表达式
num1++
num2++
具有副作用-它们更改存储在这些变量中的值。但是,您也尝试在值计算(
num1*num2
)中使用这些变量,而不使用中间序列点-程序执行中
++
的副作用已应用于
num1
num2
的点。C不要求从左到右计算函数参数,也不要求在计算后立即应用
++
运算符的副作用

行为被显式地称为未定义-编译器和运行时环境都不需要以任何特定的方式处理这种情况

要实现您想要的功能,您需要将对
num1
num2
的更新分开:

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

在什么情况下需要这样做?避免使用带有副作用的表达式作为函数调用的参数不是更清楚吗?这回答了你的问题吗?这是一种未定义的行为。C中未定义求值顺序,注:未定义≠ 未指明
for ( num1 = 0, num2 = 0; num1 < 30; num1++, num2++ )
  printf( "%d x %d = %d\n", num1, num2, num1 * num2 );