TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
%= (modulo assignment) operator is a compound assignment operator that computes the remainder of the division of its left operand by its right operand, and subsequently assigns that resulting value back to the left operand.
Syntax and Equivalence
rvalue are strictly required in this equivalence due to operator precedence. Without them, an expression like d %= e + 1 would incorrectly expand to d = d % e + 1 (which evaluates as (d % e) + 1), rather than the correct d = d % (e + 1).
Additionally, in the compound assignment (%=), the lvalue is evaluated exactly once. This is a critical distinction from the expanded form when the left operand contains side effects (e.g., array[i++] %= 2;).
Technical Constraints
- Operand Types: Both the left and right operands must be of integral types (e.g.,
char,short,int,long,long long, or theirunsignedvariants). - Floating-Point Restriction: Applying
%=to floating-point types (float,double) violates C language constraints and will result in a compilation error. - Modifiable Lvalue: The left operand must be a modifiable lvalue. It cannot be a
const-qualified variable or a literal. - Associativity: Like all assignment operators in C,
%=has right-to-left associativity.
Evaluation Rules and Edge Cases
- Sign of the Remainder: Under the C99 standard and later, the truncation of integer division is always toward zero. Consequently, the sign of the result of a modulo operation always matches the sign of the dividend (the left operand). The sign of the divisor (the right operand) does not affect the sign of the result.
- Undefined Behavior:
- Division by Zero: If the right operand evaluates to
0, the operation triggers undefined behavior, typically manifesting as a hardware exception. - Unrepresentable Quotient: If the quotient of the underlying division is not representable, the behavior of both the division (
/) and modulo (%) operators is undefined (C11 6.5.5p6). For two’s complement signed integers, this occurs if the minimum representable value of a type is moduloed by-1(e.g.,INT_MIN %= -1). Because the mathematical quotient (INT_MAX + 1) cannot be represented in the signed type, it triggers undefined behavior.
- Division by Zero: If the right operand evaluates to
Mechanical Examples
Master C with Deep Grasping Methodology!Learn More





