Absolute Value in C
Introduction
The absolute value of a number on the number line indicates its distance from 0 despite taking direction into account. Since distance can never be negative, a number's absolute value always remains positive.
For Example, -11 equals 11 in absolute terms. The absolute value of 1 is 1, and the absolute value of zero is zero. In the same way, the absolute value of -14.5 is 14.5.
In the C programming language, "Absolute value" is the full form of the abs function.
abs() Function in C
“The function abs in C returns an integer's absolute value in a program written in the programming dialect C.” The term "integer" denotes how the abs() function is mathematical.
To calculate the absolute value of integers, use the built-in function abs() in the stdlib.h package.
The syntax used for abs() is:
int abs (int y);
Parameters of abs():
The parameters of the abs() function are any integer value or integer variable. It is y in the above syntax.
Return value in abs():
The return value of the absolute function abs() in C is the absolute value of an integer, which we provide as the argument. The absolute value of y is the abs() return value in the above syntax.
Let us look at an example of code using the abs() function:
Code
#include<stdio.h>
#include<stdlib.h>
int main()
{
int m;
printf("Enter an integer number: ");
scanf("%d",&m);
m=abs(m);
printf("The Absolute value is given by : %d\n", m);
return 0;
}
Output
![Absolute Value in C](https://static.tutorialandexample.com/c/absolute-value-in-c1.png)
fabs() function in C
In the C language, it can be used to determine the absolute value of a floating-point number. It is provided in the "math.h" header file.
The function syntax is given by:
double fabs (double y);
Parameters used:
Parameters used in fabs() are double values or double variables.
Return value:
The given parameter's absolute value is of double type.
Example program using the fabs() function:
Code
#include<stdio.h>
#include<math.h>
int main()
{
double num;
double r;
printf("Enter a decimal number: ");
scanf("%lf", &num);
r = fabs(num);
printf("Absolute value is equal to : %lf\n", r);
return 0;
}
Output
![Absolute Value in C](https://static.tutorialandexample.com/c/absolute-value-in-c2.png)
labs() function in C
For computing the absolute value of a long integer number in the C language, use the labs() function specified in the "stdlib.h" header file.
The labs() syntax is given as:
long int labs (long int x);
Parameters used in the labs() function:
In the labs() function, we use parameters like long int values or variables.
Return value:
The labs() function returns the given parameter's absolute value in the long int type.
An example program using the labs() function
Code
#include<stdio.h>
#include<stdlib.h>
int main()
{
long int s;
printf("Enter a long integer number: ");
scanf("%ld", &s);
s = abs(s);
printf("The Absolute value can be given as : %ld\n", s);
return 0;
}
Output
![Absolute Value in C](https://static.tutorialandexample.com/c/absolute-value-in-c3.png)
Absolute Value in C without using the predefined function abs()
Let us understand this with an example program.
Code
#include <stdio.h>
int main()
{
int a1;
printf("Enter a number :");
scanf ("%d", &a1);
if(a1 < 0)
{
a1 = (-1) * a1;
printf("Absolute value is: %d " ,a1);
}
else
{
printf("Absolute value is %d" ,a1);
}
return 0;
}
Output
![Absolute Value in C](https://static.tutorialandexample.com/c/absolute-value-in-c4.png)
The if-else block is used in this C program. In the if block statement, if the number is negative, it will change to a positive number; otherwise, it will stay the same. The minus sign (-) can change a number from negative to positive. In the else block statement, if the number is greater than 0, i.e., a positive number, it is returned as it is. The printf function is then used to show the outcome on the display screen.
Now let's consider a program to print the absolute values between two integers.
Code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, x, y;
printf ("Enter first digit:");
scanf (" %d", &x);
printf ("\n Enter last digit:");
scanf (" %d", &y);
for (i = x; i <= y; i++)
{
printf( "\n The absolute value of %d is %d. ", i, abs(i));
}
return 0;
}
Output
![Absolute Value in C](https://static.tutorialandexample.com/c/absolute-value-in-c5.png)
Conclusion
In C programming, you can employ the function abs to get an integer's absolute value.
When a function returns an integer's positive value, it is said to be doing so at its absolute value.
Any integral number is the parameter or argument for the C function abs. One requires a header file called "stdlib.h" to utilize C's abs() function. With abs(), the integer's absolute value is returned.