Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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).
result = dividend % 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.
int a = 10 % 3;       // Valid: Evaluates to 1
double b = 10.0 % 3;  // Invalid: 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:
(a / b) * b + (a % b) == a
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.
int r1 =  10 %  3;  // Evaluates to  1
int r2 = -10 %  3;  // Evaluates to -1 (Matches sign of -10)
int r3 =  10 % -3;  // Evaluates to  1 (Matches sign of 10)
int r4 = -10 % -3;  // Evaluates to -1 (Matches sign of -10)

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.
int a = -5;
unsigned int b = 3;

// 'a' is implicitly converted to unsigned int before evaluation.
// The bit pattern of -5 is interpreted as a large unsigned integer.
// The result type is unsigned int.
unsigned int result = a % b; 

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.
#include <limits.h>

int x = 5 % 0;         // Undefined Behavior: Division by zero
int y = INT_MIN % -1;  // Undefined Behavior: Quotient (INT_MAX + 1) is not representable
Master C with Deep Grasping Methodology!Learn More