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
🌐
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.

🌐
Scaler
scaler.com › home › topics › abs() function in c
abs() Function in C - Scaler Topics
March 21, 2024 - In the C programming language, ... the abs() is an arithmetics function. The stdlib.h library contains predefined function abs() for computing the absolute value of integers....
🌐
Arduino
docs.arduino.cc › language-reference › en › functions › math › abs
abs() function
Arduino programming language can be divided in three main parts: functions, values (variables and constants), and structure · For controlling the Arduino board and performing computations
🌐
Cppreference
en.cppreference.com › w › c › numeric › math › abs.html
abs, labs, llabs, imaxabs - cppreference.com
May 25, 2024 - In 2's complement systems, the absolute value of the most-negative value is out of range, e.g. 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 }
🌐
Vultr
docs.vultr.com › clang › standard-library › math-h › fabs
C math.h fabs() - Get Absolute Value | Vultr Docs
December 12, 2024 - This code snippet demonstrates obtaining the absolute value of the value variable. The function fabs() correctly converts -23.45 to 23.45. Initialize variables with positive and zero values. Apply fabs() to these values. Output the results to verify correctness. ... #include <stdio.h> #include <math.h> int main() { double positiveValue = 54.76; double zeroValue = 0.0; printf("Absolute value of %.2f is %.2f\n", positiveValue, fabs(positiveValue)); printf("Absolute value of %.2f is %.2f\n", zeroValue, fabs(zeroValue)); return 0; } Explain Code
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_abs.htm
C library - abs() function
Using the 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); }
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › program-to-find-absolute-value-of-a-given-number
Program to find absolute value of a given number - GeeksforGeeks
For any positive number, the absolute value is the number itself and for any negative number, the absolute value is (-1) multiplied by the negative number ... Below is the implementation of the above approach.
Published   July 15, 2025
Find elsewhere
🌐
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 ...
🌐
Marzer
marzer.github.io › muu › group__abs.html
Math » abs() module | muu Miscellaneous useful utilities for C++
Returns the absolute value of a long long. Returns the absolute value of an integral type.
🌐
Cplusplus
cplusplus.com › reference › cmath › abs
cmath's abs
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 int values).
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › stdlib_h › abs.php
C Language: abs function (Absolute Value of Integer)
The abs function returns the absolute value of an integer represented by x. In the C Language, the required header for the abs function is: #include <stdlib.h> In the C Language, the abs function can be used in the following versions: ANSI/ISO 9899-1990 · Other C functions that are similar ...
🌐
GeeksforGeeks
geeksforgeeks.org › c language › fabs-function-in-c
fabs() Function in C - GeeksforGeeks
July 23, 2025 - Parameter: It will take a single parameter which is to be converted to an absolute value. Return Value: While passing values to fabs(), we can convert the number explicitly to double. ... Example 1: Below is the C program to show the use of the fabs() function. ... // 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; }
🌐
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 ·
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › math_h › fabs.php
C Language: fabs function (Absolute Value of Floating-Point Number)
/* Example using fabs by TechOnTheNet.com */ #include <stdio.h> #include <math.h> int main(int argc, const char * argv[]) { /* Define temporary variables */ double value; double result; /* Assign the value we will find the fabs of */ value = -2.1; /* Calculate the absolute value of value */ result = fabs(value); /* Display the result of the calculation */ printf("The Absolute Value of %f is %f\n", value, result); return 0; } When compiled and run, this application will output: The Absolute Value of -2.100000 is 2.100000 ·
🌐
Wikipedia
en.wikipedia.org › wiki › Absolute_value
Absolute value - Wikipedia
2 weeks ago - {\displaystyle |0|=0} . For example, the absolute value of 3 is 3, and the absolute value of −3 is also 3. The absolute value of a number may be thought of as its distance from zero.
🌐
Cuemath
cuemath.com › algebra › absolute-value-function
Absolute Value Function - Definition, Equation, Examples| Graphing Absolute Value Functions
Generally, we can represent the absolute value function as, f(x) = a |x - h| + k, where a represents how far the graph stretches vertically, h represents the horizontal shift and k represents the vertical shift from the graph of f(x) = |x|. If the value of 'a' is negative, the graph opens downwards ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › abs-labs-llabs-abs64
abs, labs, llabs, _abs64 | Microsoft Learn
October 26, 2022 - // crt_abs.c // Build: cl /W3 /TC crt_abs.c // This program demonstrates the use of the abs function // by computing and displaying the absolute values of // several numbers. #include <stdio.h> #include <math.h> #include <stdlib.h> #include <limits.h> int main( void ) { int ix = -4; long lx = -41567L; long long llx = -9876543210LL; __int64 wx = -1; // absolute 32 bit integer value printf_s("The absolute value of %d is %d\n", ix, abs(ix)); // absolute long integer value printf_s("The absolute value of %ld is %ld\n", lx, labs(lx)); // absolute long long integer value printf_s("The absolute value