C abs() Function

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

Definition

The C abs() function returns the absolute value of an int. Negative values are negated, while zero and positive values are returned unchanged. The operation works on int; use labs() or llabs() for wider integer types.

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. On valid representable input, abs() returns the nonnegative magnitude. There is no error sentinel; an unrepresentable result, such as abs(INT_MIN), has undefined behavior. The examples below move from the direct call to an edge condition and a practical use.

Syntax

Start with this concrete abs() call:

C
abs(-12);
CallWhat it does
abs(-12);Returns the absolute value of an int 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
int abs(int value);

Parameters

ParameterDescription
valueThe int value whose magnitude is needed.

Return Value

Return ValueDescription
Nonnegative intThe absolute value of value.
Undefined caseIf the positive result cannot be represented as int, behavior is undefined.

C Examples

Example 1 — Find a magnitude

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

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

int main(void) {
    int value = -27;
    printf("%d\n", abs(value));
    return 0;
}

How It Works

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

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

int main(void) {
    printf("%d\n", abs(14));
    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 abs() contract and checks the result or precondition that matters for the scenario.

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

int main(void) {
    int expected = 80, actual = 73;
    printf("difference = %d\n", abs(actual - expected));
    return 0;
}

How It Works

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

abs() returns the absolute value of an int. Negative values are negated, while zero and positive values are returned unchanged. The operation works on int; use labs() or llabs() for wider integer types.
Include <stdlib.h> before calling abs().
On valid representable input, abs() returns the nonnegative magnitude. There is no error sentinel; an unrepresentable result, such as abs(INT_MIN), has undefined behavior.
For two's-complement int, abs(INT_MIN) is usually not representable. Check before taking the absolute value when input may be INT_MIN.
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: aligned_alloc()

Continue through the C stdlib.h tutorials.

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