Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

abs - Absolute value of a whole number

&pagelevel(4)&pagelevel

Definition

#include <stdlib.h>

int abs(int i);

abs calculates the absolute value of the integer i.

Return val.

Note

|i|                  for any given integer value i.

The absolute value of the highest presentable negative number cannot be presented. If the
highest negative number (-231) is specified as argument i, the program is terminated with
an error.

Example

The following program outputs the absolute value corresponding to an input value.

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
  int i;
  printf("Please enter int value: \n");
  if (scanf("%d", &i) == 1)          /* Checks number of entries */
     printf("i = %d; ?i? = %d\n", i, abs(i));
  else
     printf("Input error! \n");
  return 0;
}

See also

cabs, fabs, labs, llabs