Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

labs — C23 arithmetic utility function

`labs(j)` from `<stdlib.h>` returns the absolute value of a `long int` argument. Its result has type `long int`; the function neither parses text nor reports an error through `errno`.

Version: C23

Syntax

long int labs(long int j);

labs(j) from <stdlib.h> returns the absolute value of a long int argument. Its result has type long int; the function neither parses text nor reports an error through errno.

The example actually calls labs(-123456789L) and prints 123456789. The L suffix keeps the argument in long rather than relying on a later widening conversion from int.

For LONG_MIN, the positive opposite is not representable as long, so behavior is undefined; checking the sign after the call is too late. Guard that case first. Use abs for int, llabs for long long, and fabs for floating-point values.

Examples

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

int main(void) {
    printf("%ld\n", labs(-123456789L));
    return 0;
}

Result

123456789

The example actually calls `labs(-123456789L)` and prints `123456789`. The `L` suffix keeps the argument in `long` rather than relying on a later widening conversion from `int`.

Sources