The C programming language provides the abs()
method in order to calculate the absolute value of a number or integer. Absolute value is a positive or non-negative value for the specified number. Event the provided value is a negative integer it is converted into positive as the absolute value.
abs() Method Syntax
The abs() method has very simple syntax where the integer value or integer variable is provided to the abs() method as a parameter. The return type of the abs() method is an integer which is the absolute value.
int abs(INT)
- INT is the integer variable or integer value.
Calculate Absolute Value of Integer Variable
In this example, we will calculate the absolute value of the integer variables -3
and 3
.
#include <stdio.h>
#include <stdlib.h>
int main () {
int a, b, c, d;
c = -3;
d = 3;
a = abs(c);
printf("Absolute value of a = %d\n", a);
b = abs(d);
printf("Absolute value of b = %d\n", b);
return(0);
}
Calculate Absolute Value of Integer
The abs() method can be also used to calculate the absolute value of the integer values -3
and 3
.
#include <stdio.h>
#include <stdlib.h>
int main () {
int a, b;
a = abs(3);
printf("Absolute value of a = %d\n", a);
b = abs(-3);
printf("Absolute value of b = %d\n", b);
return(0);
}