C stdlib abs - Syntax
int abs(int n);
The abs() function in C is used to return the absolute value or modulus |x| of a real number x is the non-negative value of x without regard to its sign.
int abs(int n);
int n |
Specifies the integer value (*Required) |
---|---|
Return int |
Returns a positive integer. |
Note: C stdlib abs supports only integer.
Convert the positive and negative integer to an absolute value.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[])
{
// Positive integer value
int n = abs(9);
printf("The absolute value of 9 is %d \n", n);
// Negative integer value
int x = abs(-6);
printf("The absolute value of -6 is %d \n", x);
return 0;
}
Try to Convert the positive and negative float to an absolute value. C only supports the integer for the absolute value conversion
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[])
{
// Positive floating point value
float n = abs(9.3);
printf("The absolute value of 9.3 is %f \n", n);
// Negative floating point value
float x = abs(-6.3);
printf("The absolute value of -6.3 is %f \n", x);
return 0;
}