integer absolute value function

You're viewing an older version of this page (#5700). View the current version.

View versions (4)

INTERFACE

#include <stdlib.h>
int abs ( int j )

DESCRIPTION

The abs function computes the absolute value of the integer j, denoted by $|j|$ and defined through:

$$|j| := \left\{ \begin{array}{ll} j, & j \geq 0, \\ -j, & j < 0. \end{array} \right.$$
(1)
Example 1
Workings

\code #include <stdio.h> #include <stdlib.h> int main() { int a = -127; printf("|a| = %d
", abs(a)); return 0; } \endcode

Solution

Output:

|a| = 127

RETURN VALUES

The abs function returns the absolute value.

SEE ALSO

labs, fabs, floor

BUGS

The absolute value of the most negative integer remains negative.