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

double d1 = fabs(-3.8951);
Answer from herohuyongtao on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_abs.htm
C library - abs() function
The C stdlib library abs() function is used to returns the absolute value of the specified number, where absolute represents the positive number. This function only returns the positive integer.
🌐
W3Schools
w3schools.com › c › ref_stdlib_abs.php
C stdlib abs() Function
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Compiler C Syllabus C Study Plan C Interview Q&A C Certificate ... The abs() function returns the absolute (positive) value of a number.
🌐
Cppreference
en.cppreference.com › w › c › numeric › math › abs.html
abs, labs, llabs, imaxabs - cppreference.com
for 32-bit 2's complement type int, INT_MIN is -2147483648, but the would-be result 2147483648 is greater than INT_MAX, which is 2147483647. ... #include <limits.h> #include <stdio.h> #include <stdlib.h> int main(void) { printf("abs(+3) = %d\n", abs(+3)); printf("abs(-3) = %d\n", abs(-3)); // printf("%+d\n", abs(INT_MIN)); // undefined behavior on 2's complement systems }
🌐
Scaler
scaler.com › home › topics › abs() function in c
abs() Function in C - Scaler Topics
March 21, 2024 - In the C programming language, the use of the function abs in C is to return the absolute value of an integer. By integer, it means that the abs() is an arithmetics function.
🌐
Reddit
reddit.com › r/cplusplus › fast abs function
r/Cplusplus on Reddit: Fast abs function
March 10, 2024 -

So, I was thinking of making fast abs function, which would be necesary to improve performance, and I had an idea to do it something like this

int abs(int input){
    return input & -0;
}

Essentially, I am trying to make a simple function that removes the sign bit. The problem is, I heard that alot of compliers would ignore this because its a zero. How could I do it that compliers wouldnt ignore it, and it would work for the intended purpose?

Edit: Thanks for all the answers the issue has been resolved!

Find elsewhere
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.

🌐
The Open Group
pubs.opengroup.org › onlinepubs › 009695399 › functions › abs.html
abs
[CX] The functionality described ... This volume of IEEE Std 1003.1-2001 defers to the ISO C standard. The abs() function shall compute the absolute value of its integer operand, i....
🌐
Arduino
docs.arduino.cc › language-reference › en › functions › math › abs
abs()
April 25, 2025 - Arduino programming language can be divided in three main parts: functions, values (variables and constants), and structure.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › abs-labs-llabs-functions-cc
abs(), labs(), llabs() functions in C/C++ - GeeksforGeeks
July 11, 2025 - The std::abs() in C++ is used to find the absolute value of given number. The value returned by std::abs() function is of type int.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.math.abs
Math.Abs Method (System) | Microsoft Learn
decimal[] decimals = { Decimal.MaxValue, 12.45M, 0M, -19.69M, Decimal.MinValue }; foreach (decimal value in decimals) Console.WriteLine($"Abs({value}) = {Math.Abs(value)}"); // The example displays the following output: // Abs(79228162514264337593543950335) = 79228162514264337593543950335 // Abs(12.45) = 12.45 // Abs(0) = 0 // Abs(-19.69) = 19.69 // Abs(-79228162514264337593543950335) = 79228162514264337593543950335 · open System let decimals = [ Decimal.MaxValue; 12.45M; 0M -19.69M; Decimal.MinValue ] for value in decimals do // The 'abs' function may be used instead.
🌐
FavTutor
favtutor.com › blogs › absolute-value-abs-cpp
Absolute Value in C++ | abs() function with code
November 21, 2022 - Learn what is abs() function is to find the absolute value in C++.with code. Check the syntax, parameters, working, and exceptions.
🌐
Linux Man Pages
man7.org › linux › man-pages › man3 › abs.3.html
abs(3) - Linux manual page
Returns the absolute value of the integer argument, of the appropriate integer type for the function.
🌐
C For Dummies
c-for-dummies.com › blog
The abs() Function | C For Dummies Blog
November 12, 2016 - For example, you subtract variable b from a, which returns the difference. The result could be positive or negative. To ensure that its positive, you use the abs() function:
🌐
Linux Hint
linuxhint.com › abs-function-c
abs function in C – Linux Hint
You may find yourself wanting positive ... “Absolute Value” inside the C programming language, and it specifies the distance of a number just on a number line beginning from 0 without taking the direction into account....
🌐
GNU
gnu.org › s › libc › manual › html_node › Absolute-Value.html
Absolute Value (The GNU C Library)
Using uabs avoids this. llabs and ... Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts. This function returns the absolute value of the floating-point number number....
🌐
Python documentation
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.14.3 documentation
3 weeks ago - They are listed here in alphabetical order. ... Return the absolute value of a number. The argument may be an integer, a floating-point number, or an object implementing __abs__(). If the argument is a complex number, its magnitude is returned.
🌐
Codecademy
codecademy.com › docs › c# › math functions › .abs()
C# (C Sharp) | Math Functions | .Abs() | Codecademy
November 19, 2025 - The C# Math.Abs() method is a static method that returns the absolute value of a specified number.
🌐
Cppreference
en.cppreference.com › w › cpp › numeric › math › abs
std::abs, std::labs, std::llabs, std::imaxabs - cppreference.com
March 14, 2025 - Common mathematical functions · [edit] Computes the absolute value of the integer number num. The behavior is undefined if the result cannot be represented by the return type. If std::abs is called with an unsigned integral argument that cannot be converted to int by integral promotion, the ...
🌐
Tutorial Gateway
tutorialgateway.org › c-abs-function
C abs function
March 25, 2025 - The C abs function is one of the Math methods used to return the absolute positive value of a given number.