> ## 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.

# C Remainder

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).

```c theme={"dark"}
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.

```c theme={"dark"}
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:

```c theme={"dark"}
(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.

```c theme={"dark"}
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.

```c theme={"dark"}
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.

```c theme={"dark"}
#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
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
