The C labs() function returns the absolute value of a long. Negative values are negated, while zero and positive values are unchanged. It is the long counterpart of abs().
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 magnitude. There is no error sentinel; LONG_MIN-like unrepresentable results are undefined. The examples below move from the direct call to an edge condition and a practical use.
Syntax
Start with this concrete labs() call:
C
labs(-1200L);
Call
What it does
labs(-1200L);
Returns the absolute value of a 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 labs(long value);
Parameters
Parameter
Description
value
The long value whose magnitude is needed.
Return Value
Return Value
Description
Nonnegative long
The absolute value.
Undefined case
The positive magnitude is not representable as long.
C Examples
Example 1 — Find a magnitude
This complete program demonstrates a distinct part of the labs() contract and checks the result or precondition that matters for the scenario.
C
#include <stdio.h>
#include <stdlib.h>
int main(void) {
long value = -27L;
printf("%ld\n", labs(value));
return 0;
}
Output
27
How It Works
Step 1. Declare a negative long value.
Step 2. Pass it to labs().
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 labs() 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 labs() contract and checks the result or precondition that matters for the scenario.
C
#include <stdio.h>
#include <stdlib.h>
int main(void) {
long expected = 80L, actual = 73L;
printf("difference = %ld\n", labs(actual - expected));
return 0;
}
Output
difference = 7
How It Works
Step 1. Compute a signed difference first.
Step 2. Use labs() 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
labs() returns the absolute value of a long. Negative values are negated, while zero and positive values are unchanged. It is the long counterpart of abs().
Include <stdlib.h> before calling labs().
Representable input returns a nonnegative long magnitude. There is no error sentinel; LONG_MIN-like unrepresentable results are undefined.
labs(LONG_MIN) is not representable on common systems and has undefined behavior. Validate that edge case before calling.
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.