Popular Tutorials
Start Learning CCertification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
C fabs()
The fabs() function returns the absolute value of a number.
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?