C 中后缀和前缀之间括号的优先级
C 运算符首选项表 指出 ()
具有更高的优先级。
代码:
# include <stdio.h>
int main()
{
int temp=2;
(temp += 23)++; //Statement 1
++(temp += 23); //Statement 2
printf("%d",temp);
return 0;
}
我的问题是,虽然语句 2 中括号的优先级高于前缀运算符,但为什么会出现错误。 在语句 1 中,两者具有相同的优先级,但求值顺序是从左到右。还是同样的错误。 第三个问题:运算符 += 的优先级低得多,那么为什么它会导致错误。
error: lvalue required as increment operand
The C Operator Preference Table notes the higher precedence of ()
.
Code:
# include <stdio.h>
int main()
{
int temp=2;
(temp += 23)++; //Statement 1
++(temp += 23); //Statement 2
printf("%d",temp);
return 0;
}
My question is while parentheses has higher precedence than pre-fix operator in Statement 2 why there's an error.
In Statement 1 both has same precedence but order of evaluation is from left to right. Still the same error.
Third doubt: operator += has much lower precedence, then why it's causing error.
error: lvalue required as increment operand
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
lvalue
是可以分配其他值的值(因为它位于赋值运算符的左侧)。(temp += 23)
是一个右值
。不能为其分配任何内容。An
lvalue
is a value that some other value can be assigned to (because it is on the left side of the assignment operator).(temp += 23)
is arvalue
. Nothing can be assigned to it.我想补充的一点是,您似乎正在尝试在表达式中多次修改某个值。根据 C99 标准 6.5(2),这是未定义的行为。
脚注 71) 显示了示例:
Something else I'd like to add, is that it looks like you're trying to modify a value more than once in an expression. That's undefined behavior according to C99 standard 6.5(2).
And footnote 71) shows the example: