Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

Remainder assignment (%=)

The %= operator stores an integer division remainder in its left operand, evaluating that operand only once.

Version: C23; repository compiler flag, production runtime not attested

Syntax

lhs %= rhs

If lhs has a non-atomic type, lhs %= rhs works like lhs = lhs % (rhs), except that lhs is evaluated only once. The operator requires integer operands and a modifiable lvalue on the left. Usual arithmetic conversions apply; the result is converted to the type of lhs.

If lhs has an atomic type, lhs %= rhs still evaluates lhs only once and performs a single sequentially consistent read-modify-write operation. In contrast, lhs = lhs % (rhs) performs a separate atomic load and atomic store.

A nonzero remainder has the converted dividend's sign, so it need not be positive. Behavior is undefined if the divisor is zero or the quotient is not representable in the calculation type.

Examples

#include <stdio.h>

int main(void)
{
    int a = 17;
    int b = -17;
    a %= 5;
    b %= 5;
    printf("%d %d\n", a, b);
    return 0;
}

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.

Sources