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.

Answer from nielsen on Stack Overflow
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ c_standard_library โ€บ c_function_abs.htm
C library - abs() function
#include<stdio.h> #include<stdlib.h> int main(){ int x, y; //absolute of negative number x = abs(-10); printf("Absolute of -10: %d\n", x); // absolute of positive number y = abs(12); printf("Absolute of 12: %d", y); } ... Create another c program ...
๐ŸŒ
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.
๐ŸŒ
Programiz
programiz.com โ€บ cpp-programming โ€บ library-function โ€บ cmath โ€บ abs
C++ cmath abs() - C++ Standard Library
#include <iostream> #include <cmath> using namespace std; int main() { double num = -87.91, result; result = abs(num); cout << "abs(" << num << ") = |" << num << "| = " << result; return 0; }
๐ŸŒ
W3Schools
w3schools.com โ€บ cpp โ€บ ref_math_abs.asp
C++ Math abs() Function
C++ Examples C++ Real-Life Examples C++ Compiler C++ Exercises C++ Quiz C++ Code Challenges C++ Syllabus C++ Study Plan 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 }
๐ŸŒ
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.
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.

Find elsewhere
๐ŸŒ
Cppreference
en.cppreference.com โ€บ w โ€บ cpp โ€บ numeric โ€บ math โ€บ abs
std::abs, std::labs, std::llabs, std::imaxabs - cppreference.com
#include <climits> #include <cstdlib> #include <iostream> int main() { std::cout << std::showpos << "abs(+3) = " << std::abs(3) << '\n' << "abs(-3) = " << std::abs(-3) << '\n'; // std::cout << std::abs(INT_MIN); // undefined behavior on 2's complement systems } ... The following behavior-changing defect reports were applied retroactively to previously published C++ standards. Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/math/abs&oldid=181910"
๐ŸŒ
Draft C++ Standard
eel.is โ€บ c++draft โ€บ c.math.abs
[c.math.abs]
๐Ÿ”—constexpr int abs(int j); constexpr long int abs(long int j); constexpr long long int abs(long long int j);
๐ŸŒ
Wikibooks
en.wikibooks.org โ€บ wiki โ€บ C_Programming โ€บ stdlib.h โ€บ abs
C Programming/stdlib.h/abs - Wikibooks, open books for an open world
These functions are defined in the standard header math.h for C and cmath for C++: int abs (int i); long labs (long i); double fabs (double i); float fabsf (float i); long double fabsl (long double i); Retrieved from "https://en.wikibooks.org/w/index.php?title=C_Programming/stdlib.h/abs&oldid=3238954" Category: Book:C Programming/stdlib.h ยท
๐ŸŒ
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.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ c-abs-function
C abs function
March 25, 2025 - #include <stdio.h> #include <math.h> int main() { printf("\n The Absolute Positive Value of 75 = %d ", abs(75)); printf("\n The Absolute Positive Value of -15 = %d ", abs(15)); printf("\n The Absolute Positive Value of -152 = %d ", abs(152)); printf("\n The Absolute Positive Value of -14 = %d ", abs(-14)); printf("\n The Absolute Positive Value of -26 = %d ", abs(-26)); printf("\n The Absolute Positive Value of 90 = %d \n", abs(90)); return 0; } In this C Programming example, we are allowing the user to enter their value.
๐ŸŒ
GNU
gnu.org โ€บ s โ€บ libc โ€บ manual โ€บ html_node โ€บ Absolute-Value.html
Absolute Value (The GNU C Library)
For a complex number z, whose real part is x and whose imaginary part is y, the absolute value is sqrt (x*x + y*y). Prototypes for abs, labs, llabs, uabs, ulabs and ullabs are in stdlib.h; imaxabs and uimaxabs are declared in inttypes.h; the fabs functions are declared in math.h; the cabs functions ...
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ cpp โ€บ c-runtime-library โ€บ reference โ€บ abs-labs-llabs-abs64
abs, labs, llabs, _abs64 | Microsoft Learn
Because C++ allows overloading, you can call overloads of abs that take and return long, long long, float, double, and long double values. These overloads are defined in the <cmath> header. In a C program, abs always takes and returns an int.
๐ŸŒ
Arduino
docs.arduino.cc โ€บ language-reference โ€บ en โ€บ functions โ€บ math โ€บ abs
abs()
Arduino programming language can be divided in three main parts: functions, values (variables and constants), and structure.
๐ŸŒ
Programming Simplified
programmingsimplified.com โ€บ c โ€บ math.h โ€บ abs
abs in C | Programming Simplified
#include <stdio.h> #include <math.h> int main() { int n, result; printf("Enter an integer to calculate its absolute value\n"); scanf("%d", &n); result = abs(n); printf("Absolute value of %d = %d\n", n, result); return 0; } Output of program: You can implement you own function as follows: long absolute(long value) { if (value < 0) return -value; return value; } C programs ยท
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c++ โ€บ abs-labs-llabs-functions-cc
abs(), labs(), llabs() functions in C/C++ - GeeksforGeeks
July 11, 2025 - The std::abs(), std::labs() and std::llabs() in C++ are built-in functions that are used to find the absolute value of any number that is given as the argument. Absolute value is the value of a number without any sign.
๐ŸŒ
C For Dummies
c-for-dummies.com โ€บ blog
The abs() Function | C For Dummies Blog
The abs() function is defined in the stdlib.h header file. It accepts integer values as input and returns a positive integer value. 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.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ c++ โ€บ math functions โ€บ abs()
C++ (C Plus Plus) | Math Functions | abs() | Codecademy
July 20, 2023 - The abs() function returns the absolute value of the argument. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!