c - `(i) = 1` 在标准 C 中是非法的吗?

标签 c language-lawyer variable-assignment rvalue lvalue-to-rvalue

我正在编写一个遵循 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) = 1i 在括号中。所以 (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/

相关文章:

c - 如何将文件名添加到系统日志字符串?

c - 将 char 指针分配给另一个位置并使用 strcpy() 时出现问题?

c++ - 如何在 C++ 中显式实例化模板 constexpr 变量?

php - 表达式中的变量赋值如何工作?

c - ABI 是 C 标准的一部分吗?

c++ - 一个有效的程序被 man7.org 宣布为无效

c++ - 在同一表达式内的表达式中使用副作用结果是否安全?

c - 通过 const 左值访问非常量对象是否安全?

c - 同等不同类型不同尺寸

C++ 我可以使用 string::at 分配一个 int 吗?