integer absolute value function

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

View versions (3)

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

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int a = -127;
  printf("|a| = %d\n", abs(a));
  return 0;
}

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.