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);
Call
What 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
Parameter
Description
value
The int value whose magnitude is needed.
Return Value
Return Value
Description
Nonnegative int
The absolute value of value.
Undefined case
If 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;
}
Output
27
How It Works
Step 1. Declare a negative int value.
Step 2. Pass it to abs().
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 abs() 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 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;
}
Output
difference = 7
How It Works
Step 1. Compute a signed difference first.
Step 2. Use abs() 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
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.