Skip to main content
The % operator in C is the binary remainder operator. It evaluates to the algebraic remainder of the division of its first operand (the dividend) by its second operand (the divisor).

Operand Constraints

The % operator strictly requires operands of integral types (e.g., char, short, int, long, long long, and their unsigned variants). Applying the % operator to floating-point types (float, double) violates language constraints and results in a compilation error.

Evaluation Rules and Sign Semantics

The behavior of the % operator is tightly coupled with integer division (/). The C99 standard mandates that integer division truncates toward zero. Consequently, the remainder operator must satisfy the following algebraic identity:
Because division truncates toward zero, the sign of the result of a % operation is guaranteed to match the sign of the dividend (the left operand), regardless of the sign of the divisor.

Type Promotion and Conversions

Standard integer promotions apply to both operands before the operation is performed. If the operands have different types, the usual arithmetic conversions are applied to establish a common type, and the result type matches this common promoted type.

Undefined Behavior

The % operator invokes undefined behavior in two specific scenarios:
  1. Division by Zero: If the second operand (the divisor) evaluates to 0.
  2. Unrepresentable Quotient (Overflow): According to the C standard, if the quotient of a / b is not representable, the behavior of both a / b and a % b is undefined. For signed integers, two’s complement arithmetic makes the absolute value of the minimum integer one greater than the maximum integer. Therefore, this occurs when the dividend is the minimum representable value for the type (e.g., INT_MIN) and the divisor is -1.
Both scenarios frequently cause a hardware-level trap (such as a floating-point exception, SIGFPE, on x86 and POSIX systems) because the underlying CPU division instruction faults, leading to abnormal program termination.
Tired of Poor C Skills? Fix That With Deep Grasping!Learn More