C Library Functions -abs()

C Standard Library - <stdlib.h>

Description

C Library Functionsint abs(int x)Returns an integer'sxabsolute value.

Note:The abs() function is only applicable to integers. If you need to calculate the absolute value of a floating-point number, you need to usefabs()function.

Declaration

Below is the declaration of the abs() function.

int abs(int x)

Parameter

  • x -- The integer whose absolute value is to be calculated.

Return Value

If x is positive, return x; if x is negative, return its opposite, i.e., -x. If x is 0, return 0.

Example

The following example demonstrates the usage of the abs() function.

Example

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

int main ()
{
   int a, b;

   a = abs(5);
   printf("value of a = %d\n", a);

   b = abs(-10);
   printf("value of b = %d\n", b);
   
   return(0);
}

Let us compile and run the above program, which will produce the following result:

a 的值 = 5
b 的值 = 10

C Standard Library - <stdlib.h>

other extensions