function

abs

<cstdlib>
          int abs (           int n );
     long int abs (      long int n );   // C++ only
long long int abs ( long long int n );   // C++11 only
Absolute value
Returns the absolute value of parameter n ( /n/ ).

In C++, this function is also overloaded in <cmath> for floating-point types (see cmath abs), in <complex> for complex numbers (see complex abs), and in <valarray> for valarrays (see valarray abs).

Parameters

n
Integral value.

Return Value

The absolute value of n.

Portability

In C, only the int version exists.
For the long int equivalent se labs.
For the long long int equivalent se llabs.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
/* abs example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int n,m;
  n=abs(23);
  m=abs(-11);
  printf ("n=%d\n",n);
  printf ("m=%d\n",m);
  return 0;
}


Output:

n=23
m=11

See also