Using abs() in C for Absolute Value
In C language, the absolute value function is abs(), which can be used to calculate the absolute value of integers, floats, or long integers. Simply input the value within the parentheses when calling the abs() function.
The sample code is as follows:
#include <stdio.h>
#include <stdlib.h>
int main() {
int num = -10;
int absNum = abs(num);
printf("The absolute value of %d is %d\n", num, absNum);
float fnum = -3.14;
float absFnum = abs(fnum);
printf("The absolute value of %.2f is %.2f\n", fnum, absFnum);
long long llnum = -1234567890;
long long absLLnum = abs(llnum);
printf("The absolute value of %lld is %lld\n", llnum, absLLnum);
return 0;
}
Output results:
The absolute value of -10 is 10
The absolute value of -3.14 is 3.14
The absolute value of -1234567890 is 1234567890