C abs function

The C abs function is one of the Math methods used to return the absolute positive value of a given number. It is very helpful in system processing, for instance, we need absolute values in signal processing, embedded control systems, etc.

In simple terms, the C abs() function checks whether the given value is a negative number. If true, negate it (convert to an equivalent positive number). If it is a positive number, return the same number (unchanged).

C abs function syntax

The syntax of the math library’s abs function, which returns the absolute value, is as shown below.

int abs(int number);

Parameters

The abs method accepts a single parameter, and it should be an integer value.

Return Value

It returns an integer data type, which is the absolute value of a given parameter.

Header File required

To use the abs() function, we must include the <math.h> header file.

#include <stdlib.h>

Or

#include <math.h>

NOTE: The abs() function does not work with floating-point numbers.

C abs Function on negative and positive numbers

The math.abs function accepts any integer value and returns the absolute positive integer of a user-given number. In this program, we will find the same and display the output.

NOTE: As we mentioned earlier, when we pass a positive number, it remains unchanged.

#include <stdio.h>
#include <math.h>

int main()
{
    printf("\n The Absolute Positive Value of 75    = %d ", abs(75));
    printf("\n The Absolute Positive Value of -15   = %d ", abs(15));
    printf("\n The Absolute Positive Value of -152  = %d ", abs(152));
    
    printf("\n The Absolute Positive Value of -14  = %d ", abs(-14));
    printf("\n The Absolute Positive Value of -26  = %d ", abs(-26));
    printf("\n The Absolute Positive Value of 90  = %d \n", abs(90));
    
    return 0;
}
C abs function example

abs() with Zero

#include <stdio.h>
#include <math.h>
int main()
{
printf("%d", abs(0));
return 0;
}
0

Absolute Value of User-Entered Number

In this C Programming example, we are allowing the user to enter their value. Next, this program utilizes the abs() math function to find the absolute value of a positive number.

#include <stdio.h>
#include <math.h>
int main()
{
int number;
printf(" Please Enter any Numeric Value: ");
scanf("%d", &number);

printf("\nThe Absolute Positive Value of %d = %d", number, abs(number));
return 0;
}
 Please Enter any Numeric Value:  -987
 The Absolute Positive Value of -987 = 987

C abs() function does not modify the original number

The math.abs() function does not modify the original number passed to it. Instead, it returns a new integer value.

  • If you use it instead of the printf() statement, it shows the absolute value. However, the original value is unchanged.
  • Correct way: We must assign the abs() function result to a new variable to see or use it for further calculations.
#include <stdio.h>
#include <math.h>

int main()
{
int n = -42;
int a = abs(n);
printf("%d", a);
printf("\n%d", abs(n));
printf("\n%d", n);
return 0;
}
42
42
-42

Using the C abs() function with floating-point numbers

As we mentioned previously, the abs() function belongs to an integer type. If you pass a floating-point number, it won’t return the absolute value.

#include <stdio.h>
#include <math.h>
int main()
{
float n = -12.5;
printf("%f", abs(n));
return 0;
}
0.000

Fix: Convert Float to Int

We can change the format specifier to convert the float to an int and return the result.

printf("%d", abs(n));
12

Fix: fabs() function

We can use the fabs() function, which accepts a floating-point number and returns the absolute value as a float.

#include <stdio.h>
#include <math.h>

int main()
{
float n = -12.5;
printf("%.2f", fabs(n));
return 0;
}
12.50

Different absolute functions

The list of functions that return the absolute values is:

  • Integers – abs()
  • Floating-point numbers –fabs()
  • Long Data Type = labs()
  • Long long Data Type = llabs()

Water System Example: Temperature Monitoring

The absolute values play a vital role in finding the difference between two values. For instance, calculating the deviation between the actual maintained temperature and the current processing values.

#include <stdio.h>
#include <math.h>

int main()
{
int actual = 48;
int current = 32;
int deviation = abs(current - actual);
printf("%d", deviation);
return 0;
}
16

abs(INT_MIN)

As we all know,

  • INT_MAX = 2147483647
  • INT_MIN = -2147483648

When we try to find the absolute value of INT_MIN, the returned value must be 2147483648, which is above the integer range. It may show unpredictable results.

One solution is to use the if statement and return the INT_MAX value for the INT_MIN.

#include <stdio.h>
#include <math.h>
#include <limits.h>

int main()
{
int n = INT_MIN;
if (n == INT_MIN)
{
printf("%d\n", INT_MAX);
}
else
{
printf("%d\n", abs(n));
}
return 0;
}
2147483647