다음을 통해 공유


abs, _abs64

절대 값을 계산합니다.

int abs( 
   int 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
__int64 _abs64( 
   __int64 n 
);

매개 변수

  • n
    정수 값입니다.

반환 값

abs 함수는 해당 매개 변수의 절대 값을 반환합니다. 반환되는 오류가 없습니다.

설명

C++는 오버로드를 허용하기 때문에 long, long long, float, double 및 long double 값을 사용하고 반환하는 abs의 오버로드를 호출할 수 있습니다. C 프로그램에서 abs는 항상 int를 사용하고 반환합니다.

Microsoft 전용

정수 형식을 사용하여 표시할 수 있는 음의 정수 범위가 해당 형식을 사용하여 표시할 수 있는 양의 정수 범위보다 크기 때문에 변환될 수 없는 abs에 인수를 제공할 수 있습니다. 인수의 절대 값이 반환 형식으로 표시될 수 없을 경우 abs 함수는 변경되지 않은 인수 값을 반환합니다. 특히, abs(INT_MIN)은 INT_MIN을 반환하고, labs(LONG_MIN)은 LONG_MIN을 반환하고 llabs(LLONG_MIN) 은 LLONG_MIN을 반환하고, _abs64(_I64_MIN)은 _I64_MIN을 반환합니다. 즉, abs 함수를 사용해서는 양수 값을 보장할 수 없습니다.

Microsoft 전용 종료

요구 사항

루틴

필수 헤더

abs

<math.h>

_abs64

<stdlib.h>

예제

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

// 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));
}
  

해당 .NET Framework 항목

System::Math::Abs

참고 항목

참조

데이터 변환

부동 소수점 지원

_cabs

fabs, fabsf

labs, llabs

imaxabs