C abs() Function

Definition
In C, abs() returns the absolute value of an int. It drops a minus sign and keeps the size of the number. abs(-5) is 5, and abs(5) stays 5.
You pass one int. The result is an int that is never negative, and zero stays zero. Include <stdlib.h>. For a double, use fabs().
Syntax
Pass the integer. Start with this call.
| Call | What it does |
|---|
abs(-5) | Drops the minus sign and returns 5. |
Include <stdlib.h>. This function is not in <math.h>.
Library declarations
int abs(int x);
long labs(long x);
long long llabs(long long x);
Parameters
| Parameter | Description |
|---|
| x | Required. The int whose sign you want to drop. |
Return Value
| Return Value | Description |
|---|
| Non-negative int | The same size, with the minus sign removed. A value that is already positive stays the same. |
C Examples
Example 1 — Absolute Value of Integers
Call abs on negative, positive, and zero values.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
printf("abs(-5) = %d\n", abs(-5));
printf("abs(5) = %d\n", abs(5));
printf("abs(0) = %d\n", abs(0));
return 0;
}
abs(-5) = 5
abs(5) = 5
abs(0) = 0
How It Works
- Include stdlib.h. That is where
abs is declared. - Negative input.
abs(-5) returns 5. - Positive input. Already non-negative, so the value stays
5. - Zero. Absolute value of zero is still zero.
Example 2 — Distance Between Two Ints
Use abs(a - b) to get a non-negative difference no matter which value is larger.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a = 10;
int b = 3;
int dist = abs(a - b);
printf("|%d - %d| = %d\n", a, b, dist);
printf("|%d - %d| = %d\n", b, a, abs(b - a));
return 0;
}
|10 - 3| = 7
|3 - 10| = 7
How It Works
- Subtract first.
a - b may be positive or negative. - Fold with abs. The sign disappears; you get the distance
7. - Order does not matter.
abs(b - a) is the same distance. - Common pattern. Useful for gaps, errors, and simple 1D distance.
Example 3 — abs vs fabs
Integers use abs; floating-point values need fabs from <math.h>.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
printf("abs(-3) = %d\n", abs(-3));
printf("fabs(-3.5) = %.1f\n", fabs(-3.5));
return 0;
}
abs(-3) = 3
fabs(-3.5) = 3.5
How It Works
- abs for ints. Declared in
stdlib.h; returns int. - fabs for doubles. Declared in
math.h; keeps the fractional part. - Do not mix them up. Passing
-3.5 to abs truncates to -3 first. - Link note. On GCC, add
-lm when you use fabs.
Frequently Asked Questions
abs() returns the absolute value of an int. abs(-5) is 5, and abs(5) stays 5.
x is required. It is the int whose sign you want to drop.
A non-negative int with the same magnitude. Zero stays zero.
Include <stdlib.h>. abs() is not declared in math.h. For a double, use fabs().
INT_MIN has no positive int counterpart, so abs(INT_MIN) overflows. Do not call it.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
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