C math library function fabs()


In this article, you will learn about C math library function fabs() that returns an absolute value of the number, with explanation and example.

C math library function fabs()

fabs() is the standard C math library function that is defined in math library math.h and it returns the absolute value of number passed as an argument.

#include<math.h>

Function prototype of C math library function fabs()

double fabs( double n );

where,

             n = floating point argument.

This function returns the absolute value of n.

Example: Program to demonstrate the use of standard C math library function fabs()

/*Use of math library function fabs*/

#include<stdio.h>
#include<math.h>

int main()
{
   double x, y;

   x = 22.3;
   y = -22.3;

   //displaying actual value
   printf("Value of x = %.2lf\n"
          "Value of y = %.2lf\n\n", x, y);

   //calculating absolute value
   printf("Absolute value of x = %.2lf\n"
          "Absolute value of y = %.2lf\n", fabs(x), fabs(y));

   return 0;
}

Output

c math function fabs

Explanation

In the above program, we have calculated the absolute value of x and y.

%.2lf is used to display decimal value up to two places.