C 中 i++ 和 (i)++ 的区别
Difference between i++ and (i)++ in C
int i = 3;
int j = (i)++;
对比
int i = 3;
int j = i ++;
以上两种情况的评估方式有区别吗?
第一种情况等同于递增右值还是未定义的行为?
i++
和 (i)++
的行为相同。 C 2018 6.5.1 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.
C 1999 中的措辞相同。
在您 i++
与 (i)++
的简单示例中,没有区别,如 Eric Postpischil 的回答所述。
但是,如果您使用 *
运算符取消引用指针变量并使用递增运算符,则这种差异实际上是有意义的; *p++
和 (*p)++
是有区别的。
前面的语句取消引用指针,然后递增指针本身;后一条语句取消引用指针,然后递增取消引用的值。
int i = 3;
int j = (i)++;
对比
int i = 3;
int j = i ++;
以上两种情况的评估方式有区别吗?
第一种情况等同于递增右值还是未定义的行为?
i++
和 (i)++
的行为相同。 C 2018 6.5.1 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.
C 1999 中的措辞相同。
在您 i++
与 (i)++
的简单示例中,没有区别,如 Eric Postpischil 的回答所述。
但是,如果您使用 *
运算符取消引用指针变量并使用递增运算符,则这种差异实际上是有意义的; *p++
和 (*p)++
是有区别的。
前面的语句取消引用指针,然后递增指针本身;后一条语句取消引用指针,然后递增取消引用的值。