Namespaces
Variants
Views
Actions

abs, labs, llabs, imaxabs

From cppreference.com
< c‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
abslabsllabsimaxabs
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)(C99)(C99)
Exponential functions
(C99)
(C99)
(C99)
(C99)
Power functions
(C99)
(C99)
Trigonometric and hyperbolic functions
(C99)
(C99)
(C99)
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Nearest integer floating point operations
(C99)(C99)(C99)
(C99)
(C99)(C99)(C99)
Floating point manipulation functions
(C99)(C99)
(C99)
(C99)
Classification
(C99)
(C99)
(C99)
Macro constants
 
Defined in header <stdlib.h>
int        abs( int n );
long       labs( long n );
long long llabs( long long n );
(since C99)
Defined in header <inttypes.h>
intmax_t imaxabs( intmax_t n );
(since C99)

Computes the absolute value of an integer number.

Contents

[edit] Parameters

n - integer value

[edit] Return value

The absolute value of n (i.e. |n|)

[edit] Example

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
 
int main()
{
    printf("%d\n", abs(+3));
    printf("%d\n", abs(-3));
 
    /* Two’s complement arithmetic cannot represent the   */
    /* absolute value of the most negative integer value. */
    printf("%+d\n", INT_MIN);
    printf("%+d\n", INT_MAX);   
    printf("%+d\n", abs(INT_MIN));
 
    return 0;
}

Output:

3
3
-2147483648
+2147483647
-2147483648

[edit] See also

absolute value of a floating point value (|x|)
(function) [edit]