types_integer

types_integer (정수 타입)

이 페이지는 C++ 표준 라이브러리의 <cstdint> 헤더에 정의된 고정 폭 정수 타입과 관련 매크로에 대해 설명해요. 정확한 비트 폭을 가진 정수 타입을 제공하며, 플랫폼 간 이식성을 높이는 데 사용돼요. 또한 <cinttypes> 헤더의 서식 매크로와 함께 사용해 입출력 함수에서도 활용할 수 있어요.

출처: cppreference

본문

타입 (Types)

다음 각 typedef 이름에 대해 (optional)로 표시된 것은, 구현이 해당 타입을 제공할 때에만 정의돼요.

Defined in header <cstdint>
int8_t int16_t int32_t int64_t intN_t (optional)
int_fast8_t int_fast16_t int_fast32_t int_fast64_t int_fastN_t (optional)
int_least8_t int_least16_t int_least32_t int_least64_t int_leastN_t (optional)
intmax_t
intptr_t (optional until c++29)
uint8_t uint16_t uint32_t uint64_t uintN_t (optional)
uint_fast8_t uint_fast16_t uint_fast32_t uint_fast64_t uint_fastN_t (optional)
uint_least8_t uint_least16_t uint_least32_t uint_least64_t uint_leastN_t (optional)
uintmax_t
uintptr_t (optional until c++29)

매크로 상수 (Macro constants)

다음 각 매크로에 대해 (conditionally present)로 표시된 것은, 구현이 해당 typedef 이름을 정의할 때에만 정의돼요.

Defined in header <cstdint>
부호 있는 정수: 비트 폭
INTN_WIDTH (C++26) (conditionally present)
INT_FASTN_WIDTH (C++26) (conditionally present)
INT_LEASTN_WIDTH (C++26) (conditionally present)
INTMAX_WIDTH (C++26)
INTPTR_WIDTH (C++26) (conditionally present)
부호 있는 정수: 최솟값
INTN_MIN (conditionally present)
INT_FASTN_MIN (conditionally present)
INT_LEASTN_MIN (conditionally present)
INTMAX_MIN
INTPTR_MIN (conditionally present)
부호 있는 정수: 최댓값
INTN_MAX (conditionally present)
INT_FASTN_MAX (conditionally present)
INT_LEASTN_MAX (conditionally present)
INTMAX_MAX
INTPTR_MAX (conditionally present)
부호 없는 정수: 비트 폭
UINTN_WIDTH (C++26) (conditionally present)
UINT_FASTN_WIDTH (C++26) (conditionally present)
UINT_LEASTN_WIDTH (C++26) (conditionally present)
UINTMAX_WIDTH (C++26)
UINTPTR_WIDTH (C++26) (conditionally present)
부호 없는 정수: 최댓값
UINTN_MAX (conditionally present)
UINT_FASTN_MAX (conditionally present)
UINT_LEASTN_MAX (conditionally present)
UINTMAX_MAX
UINTPTR_MAX (conditionally present)

함수 매크로 (Function macros)

다음 각 매크로에 대해 (conditionally present)로 표시된 것은, 구현이 해당 typedef 이름을 정의할 때에만 정의돼요.

Defined in header <cstdint>
INTN_C (conditionally present)
INTMAX_C
UINTN_C (conditionally present)
UINTMAX_C
#include <cstdint>
UINT64_C(0x123) // expands to a literal of type uint_least64_t and value 0x123

서식 매크로 상수 (Format macro constants)

Defined in header <cinttypes>

std::fprintf 계열 함수를 위한 서식 상수

여기에 나열된 각 PRI 매크로는 구현이 해당 typedef 이름을 정의할 때에만 정의돼요.

int 또는 unsigned int에 해당하는 변환 설명 데이터 타입별 매크로
std::intx_t std::int_leastx_t
d 부호 있는 십진 정수 값 출력 PRIdx
i PRIix
u 부호 없는 십진 정수 값 출력 PRIux
o 부호 없는 팔진 정수 값 출력 PRIox
x 부호 없는 소문자 십육진 정수 값 출력 PRIxx
X 부호 없는 대문자 십육진 정수 값 출력 PRIXx

std::fscanf 계열 함수를 위한 서식 상수

여기에 나열된 각 SCN 매크로는 구현이 해당 typedef 이름을 정의하고 해당 타입에 적합한 std::fscanf 길이 수정자를 제공할 때에만 정의돼요.

int 또는 unsigned int에 해당하는 변환 설명 데이터 타입별 매크로
std::intx_t std::int_leastx_t
d 부호 있는 십진 정수 값 입력 SCNdx
i 부호 있는 정수 값 입력 SCNix
u 부호 없는 십진 정수 값 입력 SCNux
o 부호 없는 팔진 정수 값 입력 SCNox
x 부호 없는 십육진 정수 값 입력 SCNxx

참고 사항 (Notes)

C++는 문자열 리터럴 바로 뒤에 오는 문자를 사용자 정의 문자열 리터럴로 해석하기 때문에, printf("%" PRId64 "\n", n); 같은 C 코드는 C++에서 유효하지 않아요. PRId64 앞에 공백이 필요해요.

C99 표준은 C++ 구현이 관련 C 헤더(stdint.h 또는 inttypes.h)를 포함하기 전에 __STDC_LIMIT_MACROS, __STDC_CONSTANT_MACROS, __STDC_FORMAT_MACROS 매크로가 각각 정의되지 않는 한 위의 한계값, 상수, 서식 매크로를 정의하지 말 것을 권고했어요. 이 권고는 어떤 C++ 표준에도 채택되지 않았고 C11에서 제거되었어요. 하지만 일부 구현(예: glibc 2.17)은 이 규칙을 적용하려고 하므로 __STDC 매크로를 정의해야 할 수도 있어요. C++ 컴파일러는 어떤 상황에서는 이를 자동으로 정의하여 우회하려고 할 수 있어요.

std::int8_tsigned char일 수 있고 std::uint8_tunsigned char일 수 있지만, 둘 다 char는 될 수 없어요. char는 부호 여부와 관계없이 "signed integer type"이나 "unsigned integer type"으로 간주되지 않기 때문이에요.

예제 (Example)

이 예제에서 사용된 서식 매크로 앞의 공백에 대한 참고 사항도 함께 보세요.

#include <cinttypes>
#include <cstdio>

int main()
{
    std::printf("%zu\n", sizeof(std::int64_t));
    std::printf("%s\n", PRId64);
    std::printf("%+" PRId64 "\n", INT64_MIN);
    std::printf("%+" PRId64 "\n", INT64_MAX);
    
    std::int64_t n = 7;
    std::printf("%+" PRId64 "\n", n);
}

가능한 출력:

8
lld
-9223372036854775808
+9223372036854775807
+7

결함 보고서 (Defect reports)

다음 동작 변경 결함 보고서는 이전에 공개된 C++ 표준에 소급 적용되었어요.

DR 적용 대상 공개된 동작 올바른 동작
LWG 2820 C++11 선택적 typedef 이름과 매크로에 대한 요구 사항이 C와 일치하지 않았음 일치하도록 수정됨

참고 문헌 (References)

  • C++23 표준 (ISO/IEC 14882:2024):
    • 17.4.1 Header <cstdint> synopsis [cstdint.syn]
    • 31.13.2 Header <cinttypes> synopsis [cinttypes.syn]
  • C++20 표준 (ISO/IEC 14882:2020):
    • 17.4 Integer types [cstdint]
    • 29.12.2 Header <cinttypes> synopsis [cinttypes.syn]
  • C++17 표준 (ISO/IEC 14882:2017):
    • 21.4 Integer types [cstdint]
    • 30.11.2 Header <cinttypes> synopsis [cinttypes.syn]
  • C++14 표준 (ISO/IEC 14882:2014):
    • 18.4 Integer types [cstdint]
    • 27.9.2 C library files [c.files]
  • C++11 표준 (ISO/IEC 14882:2011):
    • 18.4 Integer types [cstdint]
    • 27.9.2 C library files [c.files]

같이 보기 (See also)

  • Fundamental types
C documentation for Fixed width integer types

더 알아보기 (Learn more)

cppreference