温馨提示×

c语言求绝对值的代码怎么写

小亿
102
2023-12-28 20:11:26
栏目: 编程语言

在C语言中,可以使用abs()函数来求一个数的绝对值。该函数的原型为:

int abs(int x);

以下是一个示例代码:

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

int main() {
   int x = -5;
   int result = abs(x);
   printf("绝对值为:%d\n", result);

   return 0;
}

运行以上代码将输出:

绝对值为:5

0