我爱橙 发表于 2022-2-28 20:57:53

NO.125 C语言赋值表达式

A错,赋值左边不能是表达式
#include <stdio.h>
#include <math.h>

int main()
{
      int d,e,f;
      d=9+e+f=d+9;
      
       printf("%d\n",d);
      

      return 0;
}
报错        lvalue required as left operand of assignment




B.不理解,e是0吗?
#include <stdio.h>
#include <math.h>

int main()
{
      int d,e,f;
      d=9+e,f=d+9;
      
       printf("%d\n%d\n",d,f);
      

      return 0;
}

输出9,18


C.
#include <stdio.h>
#include <math.h>

int main()
{
      int d,e,f;
      d=9+e,e++,d+9;
      
       printf("%d\n",d);
      

      return 0;
}

1.为什么不符合语法还能输出?
2.e是0吗?最后为什么输出9?

D
#include <stdio.h>
#include <math.h>

int main()
{
      int d,e,f;
      d=9+e++=d+7;
      
       printf("%d\n",d);
      

      return 0;
}

报错        lvalue required as left operand of assignment

大马强 发表于 2022-2-28 21:25:25

A D 都是左值问题,即等号左边不能是表达式

B
d=9+e,f=d+9;
相当于
d=9+e;// d=9+0 =9
f=d+9; // 9 +9 = 18

C 参考B, 没有不符合语法
d=9+e; // d = 9 + 0 = 9
          e++; //0++ = 1
          d+9; //9+9 =18 但是! 它没有给变量赋值 这和 d = d + 9是不一样的
页: [1]
查看完整版本: NO.125 C语言赋值表达式