abs, labs, llabs, _abs64

인수의 절대값을 계산합니다.

구문

int abs( int n );
long labs( long n );
long long llabs( long long n );
__int64 _abs64( __int64 n );
long abs( long n );   // C++ only
long long abs( long long n );   // C++ only
double abs( double n );   // C++ only
long double abs( long double n );   // C++ only
float abs( float n );   // C++ only

매개 변수

n
숫자 값입니다.

반환 값

, , 및 함수는 abs매개 변수n의 절대값을 반환합니다._abs64llabslabs 오류 반환이 없습니다.

설명

C++에서는 오버로드를 허용하므로 long, long long, float, doublelong double 값을 사용 및 반환하는 abs의 오버로드를 호출할 수 있습니다. 이러한 오버로드는 헤더에 <cmath> 정의됩니다. C 프로그램에서 abs 는 항상 int.

Microsoft 관련: 모든 정수 형식에서 나타낼 수 있는 음수 정수 범위가 해당 형식에서 나타낼 수 있는 양의 정수 범위보다 큽니다. 따라서 변환할 수 없는 이러한 함수에 인수를 제공할 수 있습니다. 인수의 절대값을 반환 형식으로 나타낼 수 없는 경우 함수는 abs 인수 값을 변경하지 않고 반환합니다. 특히 abs(INT_MIN)INT_MIN, labs(LONG_MIN)LONG_MIN, llabs(LLONG_MIN)LLONG_MIN, _abs64(_I64_MIN)_I64_MIN을 반환합니다. 실제로 함수는 abs 양수 값을 보장하는 데 사용할 수 없습니다.

요구 사항

루틴에서 반환된 값 필수 C 헤더 필수 C++ 헤더
abs, labs, llabs <math.h> 또는 <stdlib.h> <cmath>, <cstdlib>, <stdlib.h> 또는 <math.h>
_abs64 <stdlib.h> <cstdlib> 또는 <stdlib.h>

C++에서 오버로드된 버전을 abs 사용하려면 헤더를 <cmath> 포함해야 합니다.

예시

이 프로그램은 여러 숫자의 절대 값을 계산하여 표시합니다.

// crt_abs.c
// Build: cl /W3 /TC crt_abs.c
// This program demonstrates the use of the abs function
// by computing and displaying the absolute values of
// several numbers.

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

int main( void )
{
    int ix = -4;
    long lx = -41567L;
    long long llx = -9876543210LL;
    __int64 wx = -1;

    // absolute 32 bit integer value
    printf_s("The absolute value of %d is %d\n", ix, abs(ix));

    // absolute long integer value
    printf_s("The absolute value of %ld is %ld\n", lx, labs(lx));

    // absolute long long integer value
    printf_s("The absolute value of %lld is %lld\n", llx, llabs(llx));

    // absolute 64 bit integer value
    printf_s("The absolute value of 0x%.16I64x is 0x%.16I64x\n", wx,
        _abs64(wx));

    // Integer error cases:
    printf_s("Microsoft implementation-specific results:\n");
    printf_s(" abs(INT_MIN) returns %d\n", abs(INT_MIN));
    printf_s(" labs(LONG_MIN) returns %ld\n", labs(LONG_MIN));
    printf_s(" llabs(LLONG_MIN) returns %lld\n", llabs(LLONG_MIN));
    printf_s(" _abs64(_I64_MIN) returns 0x%.16I64x\n", _abs64(_I64_MIN));
}
The absolute value of -4 is 4
The absolute value of -41567 is 41567
The absolute value of -9876543210 is 9876543210
The absolute value of 0xffffffffffffffff is 0x0000000000000001
Microsoft implementation-specific results:
abs(INT_MIN) returns -2147483648
labs(LONG_MIN) returns -2147483648
llabs(LLONG_MIN) returns -9223372036854775808
_abs64(_I64_MIN) returns 0x8000000000000000

참고 항목

데이터 변환
수학 및 부동 소수점 지원
_cabs
fabs, fabsf, fabsl
imaxabs