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);
Call
What 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
Parameter
Description
value
The long long value whose magnitude is needed.
Return Value
Return Value
Description
Nonnegative long long
The absolute value.
Undefined case
The 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;
}
Output
27
How It Works
Step 1. Declare a negative long long value.
Step 2. Pass it to llabs().
Step 3. The function negates the representable negative value.
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.
Step 2. Call the matching absolute-value function.
Step 3. No sign change is required.
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;
}
Output
difference = 7
How It Works
Step 1. Compute a signed difference first.
Step 2. Use llabs() to discard its direction.
Step 3. The result is representable for these values.
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.