Fabs in c – fabs C Library Function

Fabs in c: The function double fabs(double x); returns the absolute value of x(|x|). If x is a negative number then it returns the positive equivalent of x by multiplying it with -1.

Function prototype of fabs

double fabs(double x);

  • x : A floating point value whose absolute value to be returned.

Return value of fabs

It returns the absolute value of x(|x|).

C program using fabs function

The following program shows the use of fabs function to calculate absolute value of a number.

fabs C Library Function

#include <stdio.h>
#include <math.h>
 
int main ()
{
  double x, result;
  printf("Enter a number\n");
  scanf("%lf", &x);
   
  result = fabs(x);
  printf("Absolute value of %lf is %lf\n", x, result);
   
  return 0;
}

Output

Enter a number
-123
Absolute value of -123.000000 is 123.000000
Enter a number
0
Absolute value of 0.000000 is 0.000000
Enter a number
123
Absolute value of 123.000000 is 123.000000