6.19.19 labs Function

Calculates the absolute value of a long integer.

Include

<stdlib.h>

Prototype

long labs(long i);

Argument

i
long integer value

Return Value

Returns the absolute value of i.

Remarks

A negative number is returned as positive; a positive number is unchanged.

Example

See the notes at the beginning of this chapter or section for information on using printf() or scanf() (and other functions reading and writing the stdin or stdout streams) in the example code.

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

int main(void)
{
  long i;

  i = 123456;
  printf("The absolute value of %7ld is %6ld\n", 
         i, labs(i));

  i = -246834;
  printf("The absolute value of %7ld is %6ld\n", 
         i, labs(i));

  i = 0;
  printf("The absolute value of %7ld is %6ld\n", 
         i, labs(i));
}

Example Output

The absolute value of  123456 is 123456
The absolute value of -246834 is 246834
The absolute value of       0 is      0