C llabs() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<stdlib.h>

Definition

The C llabs() function returns the absolute value of a long long. It negates negative values and leaves nonnegative values unchanged, using long long arithmetic.

This function is declared in <stdlib.h>. Its contract matters as much as its call: validate every required precondition, check any documented sentinel, and keep ownership or host effects clear in the surrounding code. Representable input returns a nonnegative long long magnitude. There is no failure sentinel; an unrepresentable magnitude is undefined. The examples below move from the direct call to an edge condition and a practical use.

Syntax

Start with this concrete llabs() call:

C
llabs(-9000000000LL);
CallWhat it does
llabs(-9000000000LL);Returns the absolute value of a long long using the concrete values shown.

Include <stdlib.h>. Also include headers that declare any types, limits, locale tools, or I/O functions used around the call.

Library declaration
C
long long llabs(long long value);

Parameters

ParameterDescription
valueThe long long value whose magnitude is needed.

Return Value

Return ValueDescription
Nonnegative long longThe absolute value.
Undefined caseThe positive magnitude is not representable.

C Examples

Example 1 — Find a magnitude

This complete program demonstrates a distinct part of the llabs() contract and checks the result or precondition that matters for the scenario.

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

int main(void) {
    long long value = -27LL;
    printf("%lld\n", llabs(value));
    return 0;
}

How It Works

  1. Step 1. Declare a negative long long value.
  2. Step 2. Pass it to llabs().
  3. Step 3. The function negates the representable negative value.
  4. Step 4. Print the nonnegative magnitude with the matching format.

Example 2 — Keep a positive value

This complete program demonstrates a distinct part of the llabs() contract and checks the result or precondition that matters for the scenario.

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

int main(void) {
    printf("%lld\n", llabs(14LL));
    return 0;
}

How It Works

  1. Step 1. Start with a positive operand.
  2. Step 2. Call the matching absolute-value function.
  3. Step 3. No sign change is required.
  4. Step 4. The original magnitude is printed.

Example 3 — Measure a difference

This complete program demonstrates a distinct part of the llabs() contract and checks the result or precondition that matters for the scenario.

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

int main(void) {
    long long expected = 80LL, actual = 73LL;
    printf("difference = %lld\n", llabs(actual - expected));
    return 0;
}

How It Works

  1. Step 1. Compute a signed difference first.
  2. Step 2. Use llabs() to discard its direction.
  3. Step 3. The result is representable for these values.
  4. Step 4. Report the distance between the two readings.

Frequently Asked Questions

llabs() returns the absolute value of a long long. It negates negative values and leaves nonnegative values unchanged, using long long arithmetic.
Include <stdlib.h> before calling llabs().
Representable input returns a nonnegative long long magnitude. There is no failure sentinel; an unrepresentable magnitude is undefined.
llabs(LLONG_MIN) is not representable on common systems and has undefined behavior. Check that value first.
The function is specified by the C standard library. Exact limits, locale behavior, environment behavior, allocation capacity, or host effects can still vary where relevant.

Next: lldiv()

Continue through the C stdlib.h tutorials.

lldiv() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful