abs Function

Calculates the absolute value.

Include

<stdlib.h>

Prototype

int abs(int i);

Argument

i
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)
{
  int i;

  i = 12;
  printf("The absolute value of  %d is  %d\n", i, abs(i));

  i = -2;
  printf("The absolute value of  %d is   %d\n", i, abs(i));

  i = 0;
  printf("The absolute value of   %d is   %d\n", i, abs(i));
}

Example Output

The absolute value of  12 is  12
The absolute value of  -2 is   2
The absolute value of   0 is   0