CKTIS / C++
Remainder assignment operator (%=)
The %= operator calculates a remainder and stores it in its left operand. The left side is evaluated only once. The built-in operator requires a modifiable integral object; a zero divisor causes undefined behavior.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
lhs %= rhsThe built-in operator lhs %= rhs stores a division remainder in lhs. It works like lhs = lhs % rhs, except that the left operand is evaluated only once. The left side must be a modifiable lvalue of integral type, such as an int variable, not a constant or an addition result. The entire expression produces an lvalue referring to the left operand.
The right operand may have integral or unscoped enumeration type. Floating-point types are not supported. The usual arithmetic conversions apply before calculating the remainder, and the result is then converted to the type of lhs. Mixing signed and unsigned types can therefore change the expected result.
Integer division discards the fractional part toward zero. A nonzero remainder has the sign of the left operand after conversions: 17 % 5 gives 2, and -17 % 5 gives -2. This is not modulo that guarantees a nonnegative result. A zero divisor and a quotient not representable in the calculation type cause undefined behavior; the latter includes an int holding std::numeric_limits<int>::min() with divisor -1.
Examples
#include <iostream>
int main() {
int positive = 17;
int negative = -17;
positive %= 5;
negative %= 5;
std::cout << positive << ' ' << negative << '\n';
}
Result
2 -2
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.