C labs() Function

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

Definition

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);
CallWhat 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

ParameterDescription
valueThe long value whose magnitude is needed.

Return Value

Return ValueDescription
Nonnegative longThe absolute value.
Undefined caseThe 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;
}

How It Works

  1. Step 1. Declare a negative long value.
  2. Step 2. Pass it to labs().
  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 labs() contract and checks the result or precondition that matters for the scenario.

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

int main(void) {
    printf("%ld\n", labs(14L));
    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 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;
}

How It Works

  1. Step 1. Compute a signed difference first.
  2. Step 2. Use labs() 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

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.

Next: ldiv()

Continue through the C stdlib.h tutorials.

ldiv() 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