C Language Undefined behavior Division by zero

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

int x = 0;
int y = 5 / x;  /* integer division */

or

double x = 0.0;
double y = 5.0 / x;  /* floating point division */

or

int x = 0;
int y = 5 % x;  /* modulo operation */

For the second line in each example, where the value of the second operand (x) is zero, the behaviour is undefined.

Note that most implementations of floating point math will follow a standard (e.g. IEEE 754), in which case operations like divide-by-zero will have consistent results (e.g., INFINITY) even though the C standard says the operation is undefined.



Got any C Language Question?