溫馨提示×

C語言絕對值函數(shù)如何調(diào)用

小億
120
2024-02-05 17:45:25
欄目: 編程語言

C語言中的絕對值函數(shù)是abs(),它可以用于計算整數(shù)、浮點數(shù)或長整數(shù)的絕對值。調(diào)用abs()函數(shù)只需在括號內(nèi)輸入需要取絕對值的值即可。

示例代碼如下:

#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;
}

輸出結(jié)果:

The absolute value of -10 is 10
The absolute value of -3.14 is 3.14
The absolute value of -1234567890 is 1234567890

0