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