abs, labs, llabs, imaxabs

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. The behavior is undefined if the result cannot be represented by the return type.


n - integer value

Return value

The absolute value of n (i.e. |n|), if it is representable.


In 2's complement systems, the absolute value of the most-negative value is out of range, e.g. for 32-bit 2's complement type int, INT_MIN is -2147483648, but the would-be result 2147483648 is greater than INT_MAX, which is 2147483647.


#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main(void)
    printf("abs(+3) = %d\n", abs(+3));
    printf("abs(-3) = %d\n", abs(-3));
//  printf("%+d\n", abs(INT_MIN)); // undefined behavior on 2's complement systems


abs(+3) = 3
abs(-3) = 3


  • C17 standard (ISO/IEC 9899:2018):
    • The imaxabs function (p: 159)
    • The abs, labs and llabs functions (p: 259)
  • C11 standard (ISO/IEC 9899:2011):
    • The imaxabs function (p: 218)
    • The abs, labs and llabs functions (p: 356)
  • C99 standard (ISO/IEC 9899:1999):
    • The imaxabs function (p: 199-200)
    • The abs, labs and llabs functions (p: 320)
  • C89/C90 standard (ISO/IEC 9899:1990):
    • The abs function
    • The labs function

See also

computes absolute value of a floating-point value (\(\small{|x|}\)|x|)
computes the magnitude of a complex number
C++ documentation for abs

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.