w3resource

C fabs() function

C fabs() function - Absolute value function

Syntax:

double fabs(double x)

The fabs() function is used to calculate the absolute value of the floating-point argument x.

Parameters:

Name Description Required /Optional
x Floating-point value. Required

Return value from fabs()

  • This function returns the absolute value of x.

Example: fabs() function

The following example shows the usage of fabs() function.


#include <math.h>
#include <stdio.h>
 
int main(void)
{
   double x, y;
 
   x = -7.837345;
   y = fabs(x); 
   printf("fabs( %lf ) = %lf\n", x, y);
   x = -12.22;
   y = fabs(x); 
   printf("\nfabs( %lf ) = %lf\n", x, y);
   x = 34.890;
   y = fabs(x); 
   printf("\nfabs( %lf ) = %lf\n", x, y);

} 

Output:

fabs( -7.837345 ) = 7.837345

fabs( -12.220000 ) = 12.220000

fabs( 34.890000 ) = 34.890000

C Programming Code Editor:

Previous C Programming: C ceil()
Next C Programming: C floor()



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

What's the point of const pointers?

const is a tool which you should use in pursuit of a very important C++ concept:

Find bugs at compile-time, rather than run-time, by getting the compiler to enforce what you mean.

Even though it does not change the functionality, adding const generates a compiler error when you're doing things you didn't mean to do. Imagine the following typo:

void foo(int* ptr)
{
    ptr = 0;// oops, I meant *ptr = 0
}

If you use int* const, this would generate a compiler error because you're changing the value to ptr. Adding restrictions via syntax is a good thing in general. Just don't take it too far -- the example you gave is a case where most people don't bother using const.

Ref : https://bit.ly/33Cdn3Q