What is the usage of the absolute value function in the C language?

In C language, you can use the standard library function abs() to calculate the absolute value of an integer. The prototype of this function is:

int abs(int x);

The integer x is the value to be calculated for its absolute value. The abs() function will return the absolute value of x, meaning it will return x if x is positive, and -x if x is negative.

For example, the abs() function can be used like this:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int num = -10;
    int abs_num = abs(num);
    
    printf("The absolute value of %d is %d\n", num, abs_num);
    
    return 0;
}

The output is:

The absolute value of -10 is 10

Therefore, the abs() function can be used to calculate the absolute value of an integer.

bannerAds