CKTIS / C
Subtraction assignment (-=)
The -= operator subtracts the right operand from the left and stores the result, evaluating the expression designating the left object only once.
Version: C23; repository compiler flag, production runtime not attested
Syntax
lhs -= rhslhs -= rhs requires a modifiable lvalue on the left. With a non-atomic left operand, it behaves like lhs = lhs - (rhs), except that the expression designating the left object is evaluated only once. This does not guarantee operand evaluation order.
With an atomic left operand, -= performs a single sequentially consistent atomic read-modify-write. The expanded assignment lhs = lhs - (rhs) uses a separate atomic load and store and can lose a concurrent update.
For arithmetic operands, the usual arithmetic conversions apply, and the result is converted to the left object's type. The operator does not prevent signed arithmetic overflow.
The left operand can also be a pointer to a complete object type, with an integer right operand: movement is measured in elements, not bytes. The result must remain within the same array or one past it.
In the example, values[i++] -= 3 changes the first element to 7 and increments i only once.
Examples
#include <stdio.h>
int main(void)
{
int values[] = {10, 20};
int i = 0;
values[i++] -= 3;
printf("%d %d %d\n", values[0], values[1], i);
return 0;
}
Result
7 20 1
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.