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 is a binary arithmetic operator that computes the quotient of its first operand (the dividend) divided by its second operand (the divisor). It is a left-associative multiplicative operator that strictly requires operands of arithmetic types (integer or floating-point).
quotient = operand1 / operand2;

Evaluation Mechanics

The behavior of the / operator is fundamentally dictated by the data types of its operands. C applies the usual arithmetic conversions before evaluating the expression to establish a common type.

1. Integer Division

If both operands possess integer types, the operator performs integer division. The result is an integer, and any fractional component is discarded. Since the C99 standard, the truncation of the algebraic quotient is strictly towards zero.
int a = 10 / 3;   // Evaluates to 3
int b = -10 / 3;  // Evaluates to -3
int c = 10 / -3;  // Evaluates to -3

2. Floating-Point Division

If at least one operand is of a floating-point type (float, double, or long double), the other operand is implicitly promoted to that floating-point type. The operator then performs floating-point division, yielding a fractional quotient.
double x = 10.0 / 4;  // '4' is promoted to double. Evaluates to 2.5
float y = 5 / 2.0f;   // '5' is promoted to float. Evaluates to 2.5f

Undefined Behavior (UB)

The / operator introduces two critical scenarios that result in Undefined Behavior: Division by Zero If the second operand evaluates to 0, the behavior is undefined. Note: If the environment conforms to IEC 60559 (IEEE 754) floating-point arithmetic (Annex F of the C standard), floating-point division by 0.0 is well-defined and yields positive or negative infinity, or NaN (if dividing 0.0 / 0.0). Integer division by zero remains strictly undefined.
int a = 5 / 0;       // Undefined Behavior
double b = 5.0 / 0;  // UB, or Infinity if Annex F is supported
Signed Integer Overflow In two’s complement representation, the absolute value of the minimum representable negative number is one greater than the maximum representable positive number. Dividing the minimum value of a signed integer type by -1 produces a mathematical result that cannot be represented in the return type, triggering undefined behavior.
#include <limits.h>

// INT_MIN is typically -2147483648
// INT_MAX is typically  2147483647
int a = INT_MIN / -1; // Undefined Behavior: 2147483648 exceeds INT_MAX
Master C with Deep Grasping Methodology!Learn More