English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C language basic tutorial

C language flow control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C language structure

C language file

C others

C language reference manual

C library function fabs() usage and example

C Standard Library <math.h>

The fabs(x) function returns the absolute value of the parameter x.

fabs() function prototype

double fabs(double x);

The fabs() function takes a parameter (in double form) and returns the absolute value of the number (in double form).

[Math] |x| = fabs(x) [C language]

To find the absolute value of an integer or floating-point number, you can explicitly convert the number to double.

 int x = 0;
 double result;
 result = fabs(double(x));

fabs() function inmath.hDefined in 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| = 0.2lf\n", x, result);
    x == 11.3;
    result = fabs(x);
    printf("|%.2lf| = 0.2lf\n", x, result);
    x = 0;
    result = fabs(x);
    printf("|%.2lf| = 0.2lf\n", x, result);
    return 0;
}

Output Result

|-1.50| =  1.50
|11.30| =  11.30
|0.00| = 0.00

C Standard Library <math.h>