Use fabs() (in math.h) to get absolute-value for double:

double d1 = fabs(-3.8951);
Answer from herohuyongtao on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c language › fabs-function-in-c
fabs() Function in C - GeeksforGeeks
July 23, 2025 - // 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; }
🌐
Programiz
programiz.com › c-programming › library-function › math.h › fabs
C fabs() - C Standard Library
Created with over a decade of experience and thousands of feedback. ... Try Programiz PRO! ... Become a certified C programmer. Try Programiz PRO! ... The fabs() function takes a single argument (in double) and returns the absolute value of that number (also in double).
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › math_h › fabs.php
C Language: fabs function (Absolute Value of Floating-Point Number)
In the C Programming Language, the fabs function returns the absolute value of a floating-point number. The syntax for the fabs function in the C Language is: double fabs(double x); x · The value to convert to an absolute value. The fabs function returns the absolute value of a floating-point ...
🌐
Cplusplus
cplusplus.com › reference › cmath › abs
Cplusplus
double abs (double x); float abs (float x);long double abs (long double x); double abs (T x); // additional overloads for integral types ... Returns the absolute value of x: |x|. These convenience abs overloads are exclusive of C++. In C, abs is only declared in <stdlib.h> (and operates on ...
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_fabs.htm
C library - fabs() function
The C math library fabs() function accepts the parameter(x) of type double that returns the absolute value of x. This function is defined under the header that calculates the absolute value of a float point number.
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 103197-absolute-value-double.html
absolute value of a double - C Board
You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much. Outside of your DOS world, your header file is meaningless. ... Indeed. You are right. Sometimes I am such an idiot. My best try so far, still slower than the standard implementation: ... double inline absolute(double number) { //printf("&#37;f ", number); unsigned long long* x = (unsigned long long*)&number; *x &= 0x7fffffffffffffff; //printf("%f \n", number); return number; }
🌐
GNU
gnu.org › software › libc › manual › html_node › Absolute-Value.html
Absolute Value (The GNU C Library)
Next: Normalization Functions, Up: Arithmetic Functions [Contents][Index] These functions are provided for obtaining the absolute value (or magnitude) of a number. The absolute value of a real number x is x if x is positive, −x if x is negative.
🌐
Cppreference
en.cppreference.com › w › cpp › numeric › math › fabs
std::abs(float), std::fabs, std::fabsf, std::fabsl - cppreference.com
The additional overloads are not required to be provided exactly as (A). They only need to be sufficient to ensure that for their argument num of integer type, std::fabs(num) has the same effect as std::fabs(static_cast<double>(num)). ... #include <cmath> #include <iostream> int main() { std::cout << "abs(+3.0) = " << std::abs(+3.0) << '\n' << "abs(-3.0) = " << std::abs(-3.0) << '\n'; // special values std::cout << "abs(-0.0) = " << std::abs(-0.0) << '\n' << "abs(-Inf) = " << std::abs(-INFINITY) << '\n' << "abs(-NaN) = " << std::abs(-NAN) << '\n'; }
Find elsewhere
🌐
O'Reilly
oreilly.com › library › view › c-in-a › 0596006977 › re57.html
fabs - C in a Nutshell [Book]
December 16, 2005 - #include <math.h> doublefabs( double x ); float fabsf( float x ); long double fabsl( long double x ); The fabs() function returns the absolute value of its floating-point argument x; if x is greater than or equal to 0, the return value is equal to x.
Authors   Peter PrinzTony Crawford
Published   2005
Pages   618
🌐
Fresh2Refresh
fresh2refresh.com › home › c programming tutorial › c – arithmetic functions › c – abs() function
C abs() function | C Arithmetic functions | Fresh2Refresh
September 23, 2020 - C abs() function:abs( ) function in C returns the absolute value of an integer. The absolute value of a number is always positive.
🌐
C For Dummies
c-for-dummies.com › blog
The abs() Function | C For Dummies Blog
For floating-point values, use the fabs() function, defined in the math.h header file. It accepts double values as input and returns a positive double value. Other variations are available on these absolute value functions; refer to the man page entries for abs() and fabs().
🌐
Tutorial Gateway
tutorialgateway.org › c-program-to-find-the-absolute-value-of-a-number
C Program to Find the Absolute Value of a Number
December 19, 2024 - Enter Double Number = -34556.8765 Enter Float Number = -2345.239f Actual Double Number = -34556.876500 Absolute Double Number = 34556.876500 Actual Float Number = -2345.239 Absolute Float Number = 2345.239 · This c example uses the labs function to find the absolute value of a long number.
Top answer
1 of 5
4

The shortest solution in your first piece of code is to change the printf statement as follows:

    printf("absValue = %u\n", (unsigned)((u<0)?-u:u));

This will print the absolute value of u. The type conversion (unsigned) ensures that the data type is as expected by printf. The statement (u<0)?-u:u uses the conditional operator to select the value -u if the condition (u<0) is true and u if the condition is false (i.e. u>=0).

The problem in your code is that u is a signed integer which means its value is stored using the Two's complement representation in 4 bytes(*) and printf is not intelligent. When you tell printf to display an unsigned integer, then printf will take the 4 bytes holding u and interpret them as an unsigned integer. Since negative numbers in Two's complement are stored as large positive integers, that is the result you see.

(*) The use of Two's complement and the int size of 4 is machine-dependent, but common.

2 of 5
4

As an alternative, you can also use the standard C function abs() (or one of its related functions):

7.22.6.1 The abs, labs and llabs functions

Synopsis

     #include <stdlib.h>
     int abs(int j);
     long int labs(long int j);
     long long int llabs(long long int j);

Description

The abs, labs, and llabs functions compute the absolute value of an integer j. If the result cannot be represented, the behavior is undefined.

Returns

The abs, labs, and llabs, functions return the absolute value.

Footnotes

The absolute value of the most negative number cannot be represented in two's complement.

Note the footnote "The absolute value of the most negative number cannot be represented in two's complement." and "If the result cannot be represented, the behavior is undefined." Strictly speaking, you'd likely need to use long long int and llabs() to avoid undefined behavior in converting INT_MIN to a positive value, assuming a 32-bit int value, and long is often 32-bits, even on 64-bit Windows.

However, since double values are likely implemented in IEEE format with 53 bits of precision, a 32-bit int value can be converted to double with no loss of precision, so you can use the fabs() function to get the absolute value of a 32-bit int value in one call:

7.12.7.2 The fabs functions

Synopsis

    #include <math.h>
    double fabs(double x);
    float fabsf(float x);
    long double fabsl(long double x);

The fabs functions compute the absolute value of a floating-point number x.

So your code would be:

#include <stdio.h>
#include <math.h>

int main (int argc, char *argv[]) {
    int u;

    scanf("%d", &u);
    printf("absValue = %u\n", (unsigned) fabs((double) u));

    return 0;
}

Note that in (unsigned) fabs((double) u), casting u to double is not strictly necessary, as the int value will be implicitly converted to a double because of the double fabs(double) function prototype from stdlib.h. But the cast back to unsigned is exremely necessary to pass the unsigned int value you want to pass to printf().

You could also do this:

#include <stdio.h>
#include <math.h>

int main (int argc, char *argv[]) {
    int u;

    scanf("%d", &u);
    unsigned int absValue = fabs(u);
    printf("absValue = %u\n", absValue);

    return 0;
}

That works because unsigned int absValue is explicitly an unsigned int.

Also, on modern CPUs, conversion between int and double is usually done by a single relatively fast instruction.

🌐
Reddit
reddit.com › r/cpp_questions › why is abs() in cmath also getting rid of my decimals?
r/cpp_questions on Reddit: why is abs() in cmath also getting rid of my decimals?
October 26, 2022 -

Hey guys, I have a very simple code, which is the following:

#include <iostream>
#include <cmath>

int main()
{
    double a = 2.1; 
    double b = abs(a); 

    std::cout << b; 
}

The output I get is b=2. Which doesn't really make sense to me. The output should just be 2.1, no?

Thanks in advance!

🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › abs-labs-llabs-abs64
abs, labs, llabs, _abs64 | Microsoft Learn
The abs, labs, llabs, and _abs64 functions return the absolute value of the parameter n. There's no error return. Because C++ allows overloading, you can call overloads of abs that take and return long, long long, float, double, and long double ...
🌐
TutorialsPoint
tutorialspoint.com › get-the-absolute-value-of-float-int-double-and-long-in-java
Get the absolute value of float, int, double and long in Java
... import java.lang.Math; public ... float values float c = 8.11f; float d = -9.32f; // declaring and initialising some double values double x = -100.01d; double y = 90.344d; // declaring and initialising some long values long r = 1234567891223l; long s = -4567891234554l; ...