In the expression a[i++] += 10, how many times is the subscript expression i++ evaluated?
Correct answer
D. Once, because a compound assignment evaluates its left operand a single time
20 views
D. Once, because a compound assignment evaluates its left operand a single time
20 views
A compound assignment is defined as E1 = E1 op E2 except that E1 is evaluated only once, so a[i++] += 10 increments i exactly one time. Writing it out as a[i++] = a[i++] + 10 would double-increment and, more seriously, be undefined for want of sequencing. The guarantee is about evaluation count, not atomicity: the read-modify-write is still unsafe across threads.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is D? Ask here and get an explanation.