labs() — Calculate long absolute value

Standards

Standards / Extensions C or C++ Dependencies

ISO C
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3

both  

Format

#include <stdlib.h>

long int labs(long int n);

General description

Calculates the absolute value of its long integer argument n. The result is undefined when the argument is equal to LONG_MIN, the smallest available long integer (-2 147 483 648). The value LONG_MIN is defined in the limits.h header file.

Returned value

Returns the absolute value of the long integer argument n.

Example

CELEBL01
⁄* CELEBL01                                      

   This example computes y as the absolute value of                             
   the long integer -41567.                                                     
                                                                                
 *⁄                                                                             
#include <stdlib.h>                                                             
#include <stdio.h>                                                              
                                                                                
int main(void)                                                                  
{                                                                               
   long x, y;                                                                   
                                                                                
   x = -41567L;                                                                 
   y = labs(x);                                                                 
                                                                                
   printf("The absolute value of %ld is %ld\n", x, y);                          
}                                                                               
Output
The absolute value of -41567 is 41567

Related information