abs() — Calculate Integer Absolute Value

Format

#include <stdlib.h>
int abs(int n);

Language Level: ANSI

Threadsafe: Yes.

Description

The abs() function returns the absolute value of an integer argument n.

Return Value

There is no error return value. The result is undefined when the absolute value of the argument cannot be represented as an integer. The value of the minimum allowable integer is defined by INT_MIN in the <limits.h> include file.

Example that uses abs()

This example calculates the absolute value of an integer x and assigns it to y.

#include <stdlib.h>
#include <stdio.h>
 
int main(void)
{
    int x = -4, y;
 
    y = abs(x);
 
    printf("The absolute value of x is %d.\n", y);
 
    /********************* Output **************************
            The absolute value of x is 4.
    *****************************************************/
}

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]