C语言++自增运算符优先级问题:int y = ++x+x+++x++;


写在前面

高手直接退出!!!!

新手看过来,如果你是搜索问题题目进来的,那你可以接着往下看了

C运算符中,++自增运算符与其他运算符的优先级比较。
请看下面这一题。
(下面这种代码写法虽然合法,但为了理解这样的代码需要花费很长时间。实际上,代码不仅仅是给机器阅读的,更重要的是给人看的。所以读者以后尽量不要写出类似下面的代码来。当然,如果是为了考试检查知识点是否掌握,故意出这样的题目。)


一、题目:

int x = 0;
int y = ++x+x+++x++;
printf("%d|%d",x,y);

二、解决步骤

1.初步分析

这是C语言中一道考察运算符优先级的题目。初步分析如下:

y = (++x)+(x++)+(x++);
//本着“++在前,先自增再使用变量;++在后,先使用变量再自增”的规则
//首先x自增1得x=1,然后y=1 + 1++ + x++;
//其次x自增1得x=2,然后y=1 + 1 + 2++,此时得出y=4;
//最后x自增1得x=3。

所以结果应该是x=3,y=4

2.上机运行验证

使用Code::Blocks 20.03运行,源码如下:

#include <stdio.h>

main()
{
    int x=0;
    int y=++x+x+++x++;

    printf("x = %d|y = %d",x,y);
}

运行截图:

运行截图
结果出人意料,只有x=3对了。x=3,无需过多解释,不论顺序如何,x一定是自增了3次,所以一定的是等于3的。但是y=5是怎么得到的呢?

3.逐步分析

  • y=++x;
  • y=++x+x++;
  • y=++x+x+++x++;
  • y=++x+x+++x+++x++;
  • y=++x+x+++x+++x+++x++;
  • y=++x+x+++x+++x+++x+++x++;

源码:

#include <stdio.h>

main()
{
    int x1=0,y1=++x1;
    int x2=0,y2=++x2+x2++;
    int x3=0,y3=++x3+x3+++x3++;
    int x4=0,y4=++x4+x4+++x4+++x4++;
    int x5=0,y5=++x5+x5+++x5+++x5+++x5++;
    int x6=0,y6=++x6+x6+++x6+++x6+++x6+++x6++;

    printf("x = %3d|%3d|%3d|%3d|%3d|%3d|nn",x1,x2,x3,x4,x5,x6);
    printf("y = %3d|%3d|%3d|%3d|%3d|%3d|nn",y1,y2,y3,y4,y5,y6);
}

运行截图:

在这里插入图片描述

  • y=x++;
  • y=x+++x++;
  • y=x+++x+++x++
  • y=x+++x+++x+++x++;
  • y=x+++x+++x+++x+++x++;
  • y=x+++x+++x+++x+++x+++x++;

源码:

#include <stdio.h>

main()
{
    int x1=0,y1=x1++;
    int x2=0,y2=x2+++x2++;
    int x3=0,y3=x3+++x3+++x3++;
    int x4=0,y4=x4+++x4+++x4+++x4++;
    int x5=0,y5=x5+++x5+++x5+++x5+++x5++;
    int x6=0,y6=x6+++x6+++x6+++x6+++x6+++x6++;

    printf("x = %3d|%3d|%3d|%3d|%3d|%3d|nn",x1,x2,x3,x4,x5,x6);
    printf("y = %3d|%3d|%3d|%3d|%3d|%3d|nn",y1,y2,y3,y4,y5,y6);
}

运行截图:

在这里插入图片描述

  • y=++x;
  • y=++x+++x;
  • y=++x+++x+++x;
  • y=++x+++x+++x+++x;
  • y=++x+++x+++x+++x+++x;
  • y=++x+++x+++x+++x+++x+++x;

源码:

#include <stdio.h>

main()
{
    int x1=0,y1=++x1;
    int x2=0,y2=++x2+++x2;
    int x3=0,y3=++x3+++x3+++x3;
    int x4=0,y4=++x4+++x4+++x4+++x4;
    int x5=0,y5=++x5+++x5+++x5+++x5+++x5;
    int x6=0,y6=++x6+++x6+++x6+++x6+++x6+++x6;

    printf("x = %3d|%3d|%3d|%3d|%3d|%3d|nn",x1,x2,x3,x4,x5,x6);
    printf("y = %3d|%3d|%3d|%3d|%3d|%3d|nn",y1,y2,y3,y4,y5,y6);
}

运行截图:

在这里插入图片描述

想要实现,++x+++x表示为++x+(++x)的意思必须加括号才可以,否则报错。故源码改为如下。

源码:

#include <stdio.h>

main()
{
    int x1=0,y1=++x1;
    int x2=0,y2=++x2+(++x2);
    int x3=0,y3=++x3+(++x3)+(++x3);
    int x4=0,y4=++x4+(++x4)+(++x4)+(++x4);
    int x5=0,y5=++x5+(++x5)+(++x5)+(++x5)+(++x5);
    int x6=0,y6=++x6+(++x6)+(++x6)+(++x6)+(++x6)+(++x6);

    printf("x = %3d|%3d|%3d|%3d|%3d|%3d|nn",x1,x2,x3,x4,x5,x6);
    printf("y = %3d|%3d|%3d|%3d|%3d|%3d|nn",y1,y2,y3,y4,y5,y6);
}

运行截图:

在这里插入图片描述


总结

这个题没有唯一答案,结尾附了问题回答链接,各个回答都很有参考意义。新手建议查看。

C/C++语言标准没有规定运算符的操作数的计算顺序,编译器可以按任意顺序来计算操作数的值,因此如果一个变量在同一个运算符的两个操作数分别被修改或一个修改一个读取,其结果在不同的编译器上可能不一样的。以后不要写这种代码,讨论也没有意义。
From https://en.cppreference.com/w/c/language/eval_order

Order of evaluation of the operands of any C operator, including the order of evaluation of function arguments in a function-call expression, and the order of evaluation of the subexpressions within any expression is unspecified (except where noted below). The compiler will evaluate them in any order, and may choose another order when the same expression is evaluated again.

这是我当时在问答里提出的问题和回答链接
问题和回答链接

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>