C abs() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
stdlib.h

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.

C
abs(-5);
CallWhat it does
abs(-5)Drops the minus sign and returns 5.

Include <stdlib.h>. This function is not in <math.h>.

Library declarations
C
int abs(int x);
long labs(long x);
long long llabs(long long x);

Parameters

ParameterDescription
xRequired. The int whose sign you want to drop.

Return Value

Return ValueDescription
Non-negative intThe 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.

C
#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;
}

How It Works

  1. Include stdlib.h. That is where abs is declared.
  2. Negative input. abs(-5) returns 5.
  3. Positive input. Already non-negative, so the value stays 5.
  4. 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.

C
#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;
}

How It Works

  1. Subtract first. a - b may be positive or negative.
  2. Fold with abs. The sign disappears; you get the distance 7.
  3. Order does not matter. abs(b - a) is the same distance.
  4. 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>.

C
#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;
}

How It Works

  1. abs for ints. Declared in stdlib.h; returns int.
  2. fabs for doubles. Declared in math.h; keeps the fractional part.
  3. Do not mix them up. Passing -3.5 to abs truncates to -3 first.
  4. 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.

Next: acos()

Compute the inverse cosine and get an angle in radians.

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