C Reference ยท stdlib.h
C abs(), labs(), llabs() and div() functions
Reference for C abs(), labs(), llabs(), div(), ldiv() and lldiv(): absolute values and quotient plus remainder in one call, with the INT_MIN trap explained.
abs(), labs() and llabs() return the absolute value of an int, long and long long. div(), ldiv() and lldiv() divide two integers and return both the quotient and the remainder in a struct.
They are small helpers, but each hides one sharp edge: the absolute value of the most negative integer does not exist, and the remainder of a division takes the sign of the dividend. For floating-point numbers use fabs() from <math.h> instead.
#include <stdlib.h> int abs(int j); div_t div(int numer, int denom);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
int abs (int j); long labs (long j); long long llabs(long long j); div_t div (int numer, int denom); ldiv_t ldiv (long numer, long denom); lldiv_t lldiv(long long numer, long long denom);
div_t, ldiv_t and lldiv_t are structures with the members quot and rem (of type int, long and long long). Their member order is unspecified, so use the names rather than positional initializers.
| Name | Description |
|---|---|
j |
The value whose absolute value is wanted. |
numer |
The dividend (numerator). |
denom |
The divisor (denominator). |
Returns: abs family: the absolute value. div family: a struct where quot is the quotient truncated toward zero and rem the remainder, so that quot * denom + rem == numer.
Errors / undefined behavior: abs(INT_MIN) (and the same for labs/llabs) is undefined behavior on two's-complement machines because the result does not fit. A zero divisor is undefined behavior. Dividing INT_MIN by -1 also overflows.
Usage notes
- Truncation toward zero has been guaranteed since C99. Before that the rounding direction for negative operands was implementation-defined, which was a reason to use
divat all. Todaya / banda % bgive the same results, sodivis mostly a convenience. - The remainder is not a mathematical modulo.
-17 % 5is-2. For array wrap-around add the divisor and take the remainder again, as in the second example. - Guard against
INT_MINwhen the input is not trusted:x == INT_MIN ? INT_MAX : abs(x)is one way to avoid the overflow, or compute in a wider type as shown in the first example. - Use the matching width. Calling
abs()on alongsilently converts it tointand can truncate; the compiler may warn with-Wconversion. abson unsigned values is pointless andabs(unsigned)converts toint. Compare the two numbers instead.- Cost: a single instruction on most compilers; you rarely gain speed from
divitself, since one division instruction normally yields both results anyway. - C23:
<stdlib.h>is unchanged for these functions;<stdckdint.h>offers checked arithmetic if you need overflow detection (seeckd_addand friends).