Division by zero
suggest changeInteger division or remainder with a zero divisor has undefined behavior:
int divisor = 0;
int quotient = 5 / divisor; /* Undefined behavior. */
int remainder = 5 % divisor; /* Undefined behavior. */
Check a value before using it as a divisor:
if (divisor != 0) {
int quotient = 5 / divisor;
/* Use quotient. */
}
For floating-point operands, the base C rules also make division by zero undefined. Implementations that define the relevant IEC 60559 floating-point semantics provide specified results and exceptions—for example, a nonzero finite value divided by zero commonly produces a signed infinity and raises the divide-by-zero exception. Do not assume those semantics unless the implementation documents the binding and exposes the corresponding feature macros.
0.0 / 0.0 is a different exceptional operation and commonly produces a NaN under IEC 60559 semantics.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents