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?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community