C fabs()

Function Prototype of fabs()

double fabs  (double x);

The fabs() function takes a single argument (in double) and returns the absolute value of that number (also in double).

[Mathematics] |x| = fabs(x) [In C programming]

To find absolute value of an integer or a float, you can explicitly convert the number to double.

 int x = 0;
 double result;
 result = fabs(double(x));

The fabs() function is defined in math.h header file


Example: C fabs() function

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

int main()
{
    double x, result;

    x = -1.5;
    result = fabs(x);
    printf("|%.2lf| =  %.2lf\n", x, result);

    x = 11.3;
    result = fabs(x);
    printf("|%.2lf| =  %.2lf\n", x, result);

    x = 0;
    result = fabs(x);
    printf("|%.2lf| =  %.2lf\n", x, result);

    return 0;

}

Output

|-1.50| =  1.50
|11.30| =  11.30
|0.00| =  0.00
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges