Open In App

fabs() Function in C

Last Updated : 09 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

fabs() function of math.h header file in C programming is used to get the absolute value of a floating point number. This function returns the absolute value in double. 

Syntax:

double fabs (double a);

Parameter: It will take a single parameter which is to be converted to an absolute value.

Return Value: While passing values to fabs(), we can convert the number explicitly to double.  

Time Complexity: O(1)
Auxiliary Space: O(1)

Example:

int marks = 90;

double percent;

percent = fabs(double(marks)); // This will convert marks to double type explicitly.

Example 1: Below is the C program to show the use of the fabs() function.

C




// C program to show the
// use of fabs() function
#include <math.h>
#include <stdio.h>
 
int main()
{
double a = 980;
double b = -1231;
double res;
 
res = fabs(a);
printf("The absolute value of %.3lf is %.3lf\n",
        a, res);
 
res = fabs(b);
printf("The absolute value of %.3lf is %.3lf\n",
        b, res);
return 0;
}


Output

The absolute value of 980.000 is 980.000
The absolute value of -1231.000 is 1231.000

Example 2: Below is the C program to show what happens when the fabs() function is used for int & long double numbers.

C




// C Program to show use of fabs() function
// with int and long double numbers
#include <math.h>
#include <stdio.h>
 
// Driver code
int main()
{
long double a = -7.546;
long double b = 4.980;
double res;
 
res = fabs(a);
printf("The absolute value of %.3lf is %.3lf\n",
        a, res);
 
res = fabs(b);
printf("The absolute value of %.3lf is %.3lf\n",
        b, res);
return 0;
}


Output

The absolute value of 7.546 is 0.000
The absolute value of 4.980 is 0.000

There are similar functions like abs() and labs() inside stdlib.h header file in C programming language.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads