C語言中的絕對值函數是abs(),它可以用于計算整數、浮點數或長整數的絕對值。調用abs()函數只需在括號內輸入需要取絕對值的值即可。
示例代碼如下:
#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;
}
輸出結果:
The absolute value of -10 is 10
The absolute value of -3.14 is 3.14
The absolute value of -1234567890 is 1234567890