표준 라이브러리 헤더 <inttypes.h>

표준 라이브러리 헤더 <inttypes.h> (C99)

printf로 정수를 찍을 때, intmax_t처럼 타입이 정확히 맞지 않으면 형식 지정자를 골라 쓰기가 까다로워요. <inttypes.h>는 이런 문제를 푸는 형식 변환 매크로와 함수를 담은 헤더예요. 타입 지원 라이브러리의 일부로, 정수 타입의 형식 변환 인터페이스를 제공하지요. 참고로 이 헤더는 <stdint.h>의 타입을 바탕으로 동작해요.

출처: cppreference

본문

<inttypes.h>는 식이 정확히 알려진 타입(intmax_t, uintmax_t)으로 확장되는 정수 변환 함수들을 선언해요.

함수

  • imaxabs(intmax_t j)intmax_t 값의 절댓값.
  • imaxdiv(intmax_t numer, intmax_t denom)intmax_t 값의 몫과 나머지(imaxdiv_t 반환).
  • strtoimax(const char* restrict nptr, char** restrict endptr, int base) — 문자열을 intmax_t로 변환.
  • strtoumax(const char* restrict nptr, char** restrict endptr, int base) — 문자열을 uintmax_t로 변환.
  • wcstoimax(const wchar_t* restrict nptr, wchar_t** restrict endptr, int base) — 와이드 문자열을 intmax_t로 변환.
  • wcstoumax(const wchar_t* restrict nptr, wchar_t** restrict endptr, int base) — 와이드 문자열을 uintmax_t로 변환.

시그니처(Synopsis)

intmax_t imaxabs(intmax_t j);
imaxdiv_t imaxdiv(intmax_t numer, intmax_t denom);
intmax_t strtoimax(const char* restrict nptr, char** restrict endptr, int base);
uintmax_t strtoumax(const char* restrict nptr, char** restrict endptr, int base);
intmax_t wcstoimax(const wchar_t* restrict nptr, wchar_t** restrict endptr, int base);
uintmax_t wcstoumax(const wchar_t* restrict nptr, wchar_t** restrict endptr, int base);

strtoimax/strtoumax는 이름과 모양이 strtol/strtoul과 똑같지만 최대 폭의 정수 타입을 다룬다는 점만 달라요. endptr에 포인터를 넘기면 변환이 멈춘 위치(문자열에서 숫자가 끝난 지점)를 받아올 수 있어요.

더 알아보기

  • 이 헤더의 타입 기반이 되는 <stdint.h>를 먼저 보면 이해가 훨씬 쉬워요.
  • 형식 지정 매크로는 주로 <stdio.h>printf/scanf 계열과 함께 쓰여요.