How to use the absolute value function in C language?

In the C language, you can use the standard library function abs() to calculate the absolute value of a number. The usage of the abs() function is as follows:

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

int main() {
    int num = -10;
    int abs_num = abs(num);
    printf("绝对值:%d\n", abs_num);
    return 0;
}

Output:

绝对值:10

In the example above, we use the abs() function to calculate the absolute value of the variable num and assign the result to the variable abs_num. Then, we use the printf() function to output the result.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds