๐ŸŒ
Programiz
programiz.com โ€บ cpp-programming โ€บ library-function โ€บ cmath โ€บ abs
C++ cmath abs() - C++ Standard Library
Become a certified C++ programmer. Try Programiz PRO! ... The abs() function in C++ returns the absolute value of the argument.
๐ŸŒ
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.
๐ŸŒ
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 }
๐ŸŒ
C For Dummies
c-for-dummies.com โ€บ blog
The abs() Function | C For Dummies Blog
November 12, 2016 - The point is that you find yourself needing positive values from time to time and the abs() function guarantees that result. For example, you subtract variable b from a, which returns the difference. The result could be positive or negative.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Cplusplus
cplusplus.com โ€บ reference โ€บ cstdlib โ€บ abs
abs
Returns the absolute value of parameter n ( /n/ ). In C++, this function is also overloaded in header <cmath> for floating-point types (see cmath abs), in header <complex> for complex numbers (see complex abs), and in header <valarray> for valarrays (see valarray abs).
๐ŸŒ
Quora
quora.com โ€บ What-is-the-function-of-the-ABS-function-in-C-programming-language
What is the function of the ABS() function in C programming language? - Quora
Answer (1 of 2): My initial inclination was to write that it exercised your abdominal muscles, making them strong enough to lift the manual containing descriptions of all the functions in the standard C library, but I realised that it might be a bit harsh. abs(x) returns the positive magnitude o...
Find elsewhere
๐ŸŒ
Linux Man Pages
man7.org โ€บ linux โ€บ man-pages โ€บ man3 โ€บ abs.3.html
abs(3) - Linux manual page
These functions compute the absolute value of the argument j of the appropriate integer type for the function.
๐ŸŒ
Draft C++ Standard
eel.is โ€บ c++draft โ€บ c.math.abs
[c.math.abs]
๐Ÿ”—constexpr floating-point-type abs(floating-point-type x);
๐ŸŒ
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.
๐ŸŒ
Cprogramming.com
cprogramming.com โ€บ fod โ€บ abs.html
abs - C/C++ Function Reference - Cprogramming.com
How to begin Get the book ยท C tutorial C++ tutorial Game programming Graphics programming Algorithms More tutorials
๐ŸŒ
Wikibooks
en.wikibooks.org โ€บ wiki โ€บ C_Programming โ€บ stdlib.h โ€บ abs
C Programming/stdlib.h/abs - Wikibooks, open books for an open world
Many programming languages have functions that calculate absolute values of numbers, either having the name abs or Abs. In languages such as C, it has variants for long integers and floating point numbers called labs and fabs.
Top answer
1 of 3
11

The standard C library is providing the optimized solutions for many problems with considerations based on the architecture, compiler in use and others. The abs() function defined in stdlib.h is one of these, and it is used for your purpose exactly. To emphasize the point, here is ARM compiler result when using abs vs a version of a homebrew abs: https://arm.godbolt.org/z/aO7t1n

Paste:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    srand(111);
    int x = rand() - 200;
    printf("%d\n", abs(x));
}

results in

main:
        push    {r4, lr}
        mov     r0, #111
        bl      srand
        bl      rand
        sub     r1, r0, #200
        cmp     r1, #0
        rsblt   r1, r1, #0
        ldr     r0, .L4
        bl      printf
        mov     r0, #0
        pop     {r4, pc}
.L4:
        .word   .LC0
.LC0:
        .ascii  "%d\012\000"

And

#include <stdio.h>
#include <stdlib.h>

int my_abs(int x)
{
    return x < 0 ? -x : x;
}

int main(void)
{
    srand(111);
    int x = rand() - 200;
    printf("%d\n", my_abs(x));
}

results in

my_abs:
        cmp     r0, #0
        rsblt   r0, r0, #0
        bx      lr
main:
        push    {r4, lr}
        mov     r0, #111
        bl      srand
        bl      rand
        sub     r1, r0, #200
        cmp     r1, #0
        rsblt   r1, r1, #0
        ldr     r0, .L5
        bl      printf
        mov     r0, #0
        pop     {r4, pc}
.L5:
        .word   .LC0
.LC0:
        .ascii  "%d\012\000"

Notice that the main code is identical (only a label name is different) in both programs as my_abs got inlined, and its implementation is the same as the standard one.

2 of 3
3

The speed of a given solution will depend greatly on the architecture, but in C I would say

return (n > 0 ? n : -n);

and let the compiler figure out the best solution.

EDIT: @jonk points out correctly that this will fail for the most-negative possible value of n, assuming that two's-complement arithmetic is used.

Yes, my solution has a conditional branch, but yours has an arithmetic operator and two bitwise operators. Can your microcontroller shift 15 places in a single clock?

๐ŸŒ
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 - 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 ...
๐ŸŒ
Cplusplus
cplusplus.com โ€บ reference โ€บ cmath โ€บ abs
std::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).
๐ŸŒ
W3Resource
w3resource.com โ€บ c-programming โ€บ stdlib โ€บ c-abs.php
C abs() function
November 12, 2025 - C abs() function (stdlib.h): The abs() function is used to compute the absolute value of an integer value.