I have understand that i = i++ is an undefined behaviour in C . But i have a doubt about a += a . Is it an undefined behaviour too ?
No, a += a
is not undefined. The behavior of i = i++
is not defined by the C standard due to this rule in C 2018 6.5 2:
If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.
That rule applies because both i++
and i =
have side effects of updating i
, and they are not sequenced. (Although the value computation of i++
, which produces the value to be used in the rest of the expression is sequenced before the assignment, its side effect of updating i
is not sequenced relative to the assignment.)
In a += a
, the value computation of the right operand ( a
) occurs before the assignment (according to 6.5.16 3), and then a +=
has the side effect of updating a
. So:
a
and a value computation of a
, but they are sequenced.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.