fabs() Function in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

The fabs in C is a library function provided within the math.h header file of the C programming language. It returns the absolute value of the given input, i.e., it will return the positive value of the given input irrespective of its sign (positive/negative).

Syntax of fabs Function in C

Parameters of fabs Function in C

The fabs function in C of the C programming language takes one argument of type double. If you give an input of any other type, say int or float. The C compiler will implicitly convert the input to double.

Return Values of fabs Function in C

The return value of the fabs in C function will always be of data type double. Suppose you try to assign the result of a fabs in C function to a variable of any other data type. In that case, the C compiler will implicitly convert the return value of data type double to that of the variable to which it is being assigned.

Exceptions of fabs Function in C

There is one scenario where the fabs in C function of the C programming language will throw an error. The fabs in C function can not only work with data type int or float inputs. It can also work with inputs of data type char. But the fabs in C cannot work with inputs of string data type. In those situations, the fabs in C will throw an error. So when using inputs of char data type, make sure you always, ALWAYS use single quotes and never double quotes.

ExampleValid / Invalid
fabs in C("a");
fabs in C('a');✔️

Example

Code

Input

Output

As we can see, fabs in C converted the negative floating value -5.0 to a positive floating value 5.000000.

More Examples of fabs Function in C

The fabs in C function can also work with inputs of int and char data types.

Example 1: fabs in C with int input

Code

Input

Output

Example 2: fabs in C with char input

Code

Output

Like fabs in C, we have abs(), labs() also in math.h which perform similar operation the only difference is that abs() work with int type data and labs() work with long int type data.

Conclusion

  • fabs in C function takes a number as input and returns the magnitude of that number, i.e., ignore the sign of the number and return its absolute value.
  • We must include the math.h header file to use the fabs in C function in C programming.
  • The input can be of the following data types, int, float, char, double for fabs in C function.
  • We can't use inputs of data type string with the fabs in C function.
  • The return value of the fabs in C function will always be that of data type double.