abs ()- 计算整数绝对值

格式

#include <stdlib.h>
int abs(int n);

语言级别

ANSI

线程安全

描述

abs() 函数返回整数自变量 n的绝对值。

返回值

没有错误返回值。 当自变量的绝对值不能表示为整数时,结果未定义。 允许的最小整数值由 <limits.h> 包含文件中的 INT_MIN 定义。

示例

此示例计算整数 x 的绝对值,并将其分配给 y
#include <stdlib.h>
#include <stdio.h>
 
int main(void)
{
    int x = -4, y;
 
    y = abs(x);
 
    printf("The absolute value of x is %d.\n", y);
 
    /********************* Output **************************
            The absolute value of x is 4.
    *****************************************************/
}

相关信息