我正在编写一个遵循 this standard 的 C 编译器,如果我像这样解析语句:
int i;
(i) = 1;
我的编译器会报告一个错误,指出 (i)
是一个右值,不应该被赋值。
我检查了代码和规则,发现了这个: 在赋值表达式语义中:
An assignment operator shall have a modifiable lvalue as its left operand.
An assignment expression has the value of the left operand after the assignment, but is not an lvalue.
在我的例子中,有两个赋值表达式:
(i) = 1
和 i
在括号中。所以 (i)
应该是一个右值。
所以我的问题是:
是
(i) = 1
在此 C 标准中非法吗?
最佳答案
引用 n1570(发布前的最后一个 C11 标准草案):
6.5.1 Primary expressions (emphasis mine)
5 A parenthesized expression is a primary expression. Its type and value are identical to those of the unparenthesized expression. It is an lvalue, a function designator, or a void expression if the unparenthesized expression is, respectively, an lvalue, a function designator, or a void expression.
i
是一个左值,所以根据上面的内容,(i)
也是。为了回答您的问题,表达式 (i) = 1
是有效的 C。
关于c - `(i) = 1` 在标准 C 中是非法的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58352605/