c语言如何求绝对值

   2025-02-13 6430
核心提示:在C语言中,可以使用abs函数来求一个整数的绝对值。#include stdio.h#include stdlib.hint main() {int num = -5;int absolute =

在C语言中,可以使用abs函数来求一个整数的绝对值。

#include <stdio.h>#include <stdlib.h>int main() {    int num = -5;    int absolute = abs(num);    printf("The absolute value of %d is %d\n", num, absolute);    return 0;}

输出结果为:

The absolute value of -5 is 5

对于浮点数的绝对值,可以使用fabs函数。

#include <stdio.h>#include <math.h>int main() {    float num = -3.14;    float absolute = fabs(num);    printf("The absolute value of %.2f is %.2f\n", num, absolute);    return 0;}

输出结果为:

The absolute value of -3.14 is 3.14

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言