Programing/C++

[C++ 프로그래밍] 자료형의 범위와 한계

유니얼 2024. 7. 7. 22:53
728x90

C++ 기초 프로그래밍: 자료형의 범위와 한계

C++ 프로그래밍에서 각 자료형의 범위와 한계를 이해하는 것은 매우 중요합니다. 자료형의 범위를 초과하는 값을 저장하면 예기치 않은 결과가 발생할 수 있기 때문입니다. C++ 표준 라이브러리의 <limits> 헤더를 사용하면 각 자료형의 최소값과 최대값을 쉽게 확인할 수 있습니다. 이번 블로그 글에서는 <limits> 헤더를 사용하여 다양한 자료형의 범위를 출력하는 방법에 대해 알아보겠습니다.

<limits> 헤더

<limits> 헤더는 C++에서 각 자료형의 다양한 특성을 정의한 템플릿 클래스인 std::numeric_limits를 제공합니다. 이를 사용하면 특정 자료형의 최소값, 최대값, 최저값 등의 정보를 얻을 수 있습니다.

예제 코드

다음 예제 코드는 여러 자료형의 범위와 한계를 출력하는 방법을 보여줍니다:

#include <iostream>
#include <limits>

int main()
{
    // 각 타입의 범위 출력
    std::cout << "The range for short is from " << std::numeric_limits<short>::min() << " to "
              << std::numeric_limits<short>::max() << std::endl;

    std::cout << "The range for unsigned short is from " << std::numeric_limits<unsigned short>::min() << " to "
              << std::numeric_limits<unsigned short>::max() << std::endl;

    std::cout << "The range for int is from " << std::numeric_limits<int>::min() << " to "
              << std::numeric_limits<int>::max() << std::endl;

    std::cout << "The range for unsigned int is from " << std::numeric_limits<unsigned int>::min() << " to "
              << std::numeric_limits<unsigned int>::max() << std::endl;

    std::cout << "The range for long is from " << std::numeric_limits<long>::min() << " to "
              << std::numeric_limits<long>::max() << std::endl;

    std::cout << "The range for float is from " << std::numeric_limits<float>::min() << " to "
              << std::numeric_limits<float>::max() << std::endl;

    // lowest()를 사용한 float 타입 범위 출력
    std::cout << "The range(with lowest) for float is from " << std::numeric_limits<float>::lowest() << " to "
              << std::numeric_limits<float>::max() << std::endl;

    // lowest()를 사용한 double 타입 범위 출력
    std::cout << "The range(with lowest) for double is from " << std::numeric_limits<double>::lowest() << " to "
              << std::numeric_limits<double>::max() << std::endl;

    // lowest()를 사용한 long double 타입 범위 출력
    std::cout << "The range(with lowest) for long double is from " << std::numeric_limits<long double>::lowest() << " to "
              << std::numeric_limits<long double>::max() << std::endl;

    // 기타 정보 출력
    std::cout << "int is signed : " << std::numeric_limits<int>::is_signed << std::endl; // int 타입이 부호를 가지는지 여부 출력
    std::cout << "int digits : " << std::numeric_limits<int>::digits << std::endl;       // int 타입의 이진수 자릿수 출력

    return 0;
}

출력 결과

위 코드를 실행하면 다음과 같은 결과를 얻을 수 있습니다:

The range for short is from -32768 to 32767
The range for unsigned short is from 0 to 65535
The range for int is from -2147483648 to 2147483647
The range for unsigned int is from 0 to 4294967295
The range for long is from -9223372036854775808 to 9223372036854775807
The range for float is from 1.17549e-38 to 3.40282e+38
The range(with lowest) for float is from -3.40282e+38 to 3.40282e+38
The range(with lowest) for double is from -1.79769e+308 to 1.79769e+308
The range(with lowest) for long double is from -1.18973e+4932 to 1.18973e+4932
int is signed : true
int digits : 31

예제 분석

자료형의 범위:

  • std::numeric_limits<자료형>::min()과 std::numeric_limits<자료형>::max()를 사용하여 각 자료형의 최소값과 최대값을 출력합니다.
  • short, unsigned short, int, unsigned int, long, float, double, long double의 범위를 출력합니다.

float, double, long double의 최저값:

  • std::numeric_limits<자료형>::lowest()를 사용하여 해당 자료형의 최저값을 출력합니다. 이는 양수 범위의 최소값인 min()과는 다릅니다.

기타 정보:

  • std::numeric_limits<int>::is_signed를 사용하여 int 자료형이 부호를 가지는지 여부를 출력합니다.
  • std::numeric_limits<int>::digits를 사용하여 int 자료형의 이진수 자릿수를 출력합니다.

결론

C++에서 각 자료형의 범위와 한계를 이해하는 것은 프로그램의 정확성과 안정성을 높이는 데 중요합니다. <limits> 헤더를 사용하면 각 자료형의 최소값, 최대값, 최저값 등의 정보를 쉽게 얻을 수 있습니다.

반응형