int i = 3;
int j = (i)++;
vs
int i = 3;
int j = i ++;
Is there a difference between how the above two cases are evaluated?
Is the first case equivalent to incrementing an rvalue or is it undefined behaviour?
int i = 3;
int j = (i)++;
vs
int i = 3;
int j = i ++;
Is there a difference between how the above two cases are evaluated?
Is the first case equivalent to incrementing an rvalue or is it undefined behaviour?
i++
and (i)++
behave identically. C 2018 6.5.1 5 says:
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.
The wording is the same in C 1999.
(*ptr)++
. –
Zolly In your simple example of i++
versus (i)++
, there is no difference, as noted in Eric Postpischil's answer.
However, this difference is actually meaningful if you are dereferencing a pointer variable with the *
operator and using the increment operator; there is a difference between *p++
and (*p)++
.
The former statement dereferences the pointer and then increments the pointer itself; the latter statement dereferences the pointer then increments the dereferenced value.
#define postinc(x) x++
then postinc(*p)
will not do what you expect given the function-like notation. That's why in writing macros it's common to heavily parenthesize e.g. #define postinc(x) ((x)++)
–
Daub (expr)++
is to increment a dereferenced value, so giving beginners the impression that expr++
is equivalent is detrimental. –
Demarcate *p++
is equivalent to *(p++)
? –
Analyse *p++
without changing the meaning? –
Analyse (*p++);
–
Demarcate © 2022 - 2024 — McMap. All rights reserved.
j
. – Muumuu