本文介绍了为什么需要左值作为增量操作数错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在左值 a = b +(++ c ++); 中需要左值作为增量操作数?

Why lvalue required as increment operand Error In a=b+(++c++); ?

只想将'b +(c + 1)'分配给'a'并同时将'C'递增2.

我只是一个初学者,只是想知道实际上是什么"LVALUE错误"?

main()
{

int a=1,b=5,c=3;

a=b+(++c++);

printf("a=%d   b= %d   c= %d \n",a,b,c);
}

推荐答案

后缀增量比前缀增量绑定得更紧密,因此您想要的是:

Postfix increment binds tighter than prefix increment so what you would want would be something like:

a = b + (++c)++;

这不是合法的C,因为前缀增加的结果(如示例中的后缀增加的结果)不是 lvalue .这意味着它只是一个价值;它不再引用像'c'这样的特定对象,因此尝试对其进行更改是没有意义的.不会有可见效果,因为不会更新任何对象.

This is not legal C, though, as the the result of prefix increment (like the result of postfix increment in your example) is not an lvalue. This means that it's just a value; it doesn't refer to a particular object like 'c' any more so trying to change it makes no sense. It would have no visible effect as no object would be updated.

我个人认为,在任何情况下,用两个语句完成操作都更加清晰.

Personally I think that doing it in two statements is clearer in any case.

a = b + c + 1;
c += 2;

这篇关于为什么需要左值作为增量操作数错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 21:50