ヘッダー <inttypes.h> は、ヘッダー <stdint.h> をインクルードしホストされた実装によって提供される追加機能で <stdint.h> を拡張します[1]

<inttypes.h> では、最大幅の整数を操作する関数と数値文字列を最大幅の整数に変換する関数を宣言し、imaxdiv関数が返す値の型である構造体の型 imaxdiv_t を宣言しています。

<inttypes.h> では、宣言された各型には、フォーマットされた入出力関数で使用する変換指定子のための対応するマクロが定義されています。

フォーマット指定用マクロ 編集

<inttypes.h> では、以下のフォーマット指定用マクロが定義されています[2]

以下のオブジェクトライクなマクロは、変換指定子を含む文字列リテラルに展開します。 変換指定子を含む文字列リテラルに展開され、長さ修飾子によって修正されることもあります。 これは、対応する整数型に変換する際に、フォーマットされた入出力関数の format 引数内で使用するのに適しています。

これらのマクロ名は、PRI (fprintf および fwprintf ファミリの文字列リテラル) または SCN (fscanf および fwscanf ファミリの文字列リテラル),220) の後に変換指定子が続き、その後に 7.20.1 の類似した型名に対応する名前が続くという一般的な形式をとる。これらの名前では、Nは§7.20.1 Integer types に記載されている型の幅を表す。例えば、PRIdFAST32 は、int_fast32_t型の整数の値を表示するためのフォーマット文字列として使用できる。

符号付き整数用のfprintfマクロ
PRIdN PRIdLEASTN PRIdFASTN PRIdMAX PRIdPTR
PRIiN PRIiLEASTN PRIiFASTN PRIiMAX PRIiPTR
符号なし整数用のfprintfマクロ
PRIoN PRIoLEASTN PRIoFASTN PRIoMAX PRIoPTR
PRIuN PRIuLEASTN PRIuFASTN PRIuMAX PRIuPTR
PRIxN PRIxLEASTN PRIxFASTN PRIxMAX PRIxPTR
PRIXN PRIXLEASTN PRIXFASTN PRIXMAX PRIXPTR
符号付き整数用のfscanfマクロ
SCNdN SCNdLEASTN SCNdFASTN SCNdMAX SCNdPTR
SCNiN SCNiLEASTN SCNiFASTN SCNiMAX SCNiPTR
符号なし整数用のfscanfマクロ
SCNoN SCNoLEASTN SCNoFASTN SCNoMAX SCNoPTR
SCNuN SCNuLEASTN SCNuFASTN SCNuMAX SCNuPTR
SCNxN SCNxLEASTN SCNxFASTN SCNxMAX SCNxPTR

(言語処理系は)実装が<stdint.h>で提供している各型について、対応するfprintfマクロを定義し、実装がその型に適したfscanf長さ修飾子を持たない限り、対応するfscanfマクロを定義しなければならない。

#include <stdio.h>
#include <inttypes.h>
int main(void)
{
  uintmax_t i = UINTMAX_MAX; // this type always exists
  wprintf(L"The largest integer value is %#022" PRIxMAX "\n", i);
  return 0;
}
実行結果
The largest integer value is 0x0000ffffffffffffffff

最大幅整数型のための関数 編集

この節は書きかけです。この節を編集してくれる方を心からお待ちしています。

最大幅の整数を操作する関数[3]

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

7.8.2 Functions for greatest-width integer types

imaxabs関数 編集

ISO/IEC 9899:2017 § 7.8.2.1 The imaxabs function[4]

形式
1 #include <inttypes.h>
intmax_t imaxabs(intmax_t j);
機能
imaxabs関数は、整数jの絶対値を計算します。
返却値
imaxabs関数は、絶対値を返します。

imaxdiv関数 編集

ISO/IEC 9899:2017 § 7.8.2.2 The imaxdiv function[5]

形式
1 #include <inttypes.h>
imaxdiv_t imaxdiv(intmax_t numer, intmax_t denom);
機能

2 The imaxdiv function computes numer / denom and numer % denom in a single operation.

返却値

3 The imaxdiv function returns a structure of type imaxdiv_t comprising both the quotient and the remainder. The structure shall contain (in either order) the members quot (the quotient) and rem (the remainder), each of which has type intmax_t. If either part of the result cannot be represented,the behavior is undefined.

strtoimax関数とstrtoumaximaxdiv関数 編集

strtoumax関数 7.8.2.3 The strtoimax and strtoumax functions

形式
1 #include <inttypes.h>
intmax_t strtoimax(const char * restrict nptr,char ** restrict endptr, int base);
uintmax_t strtoumax(const char * restrict nptr,char ** restrict endptr, int base);
機能

2 The strtoimax and strtoumax functions are equivalent to the strtol, strtoll, strtoul, and strtoull functions, except that the initial portion of the string is converted to intmax_t and uintmax_t representation, respectively.

返却値

3 The strtoimax and strtoumax functions return the converted value, if any. If no conversion could be performed, zero is returned. If the correct value is outside the range of representable values,INTMAX_MAX, INTMAX_MIN, or UINTMAX_MAX is returned (according to the return type and sign of the value, if any), and the value of the macro ERANGE is stored in errno.

wcstoimax関数とwcstoumax関数 編集

wcstoumax関数 7.8.2.4 The wcstoimax and wcstoumax functions

形式
1 #include <stddef.h> // for wchar_t
#include <inttypes.h>
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);
機能

2 The wcstoimax and wcstoumax functions are equivalent to the wcstol, wcstoll, wcstoul, and wcstoull functions except that the initial portion of the wide string is converted to intmax_t and uintmax_t representation, respectively.

返却値

3 The wcstoimax function returns the converted value, if any. If no conversion could be performed, zero is returned. If the correct value is outside the range of representable values, INTMAX_MAX, INTMAX_MIN, or UINTMAX_MAX is returned (according to the return type and sign of the value, if any),and the value of the macro ERANGE is stored in errno.

具体的な値の例 編集

<inttypes.h> で定義されているマクロの値(一例)
*8 *16 *32 *64 *LEAST8 *LEAST16 *LEAST32 *LEAST64 *FAST8 *FAST16 *FAST32 *FAST64 *MAX *PTR
PRId* d d d ld d d d ld d ld ld ld ld ld
PRIi* i i i li i i i li i li li li li li
PRIo* o o o lo o o o lo o lo lo lo lo lo
PRIu* u u u lu u u u lu u lu lu lu lu lu
PRIx* x x x lx x x x lx x lx lx lx lx lx
PRIX* X X X lX X X X lX X lX lX lX lX lX
SCNd* hhd hd d ld hhd hd d ld hhd ld ld ld ld ld
SCNi* hhi hi i li hhi hi i li hhi li li li li li
SCNo* hho ho o lo hho ho o lo hho lo lo lo lo lo
SCNu* hhu hu u lu hhu hu u lu hhu lu lu lu lu lu
SCNx* hhx hx x lx hhx hx x lx hhx lx lx lx lx lx
実際値を表示するプログラム
#include <stdio.h>
#include <inttypes.h>

int main(void) {
  puts("{| class='wikitable' style='text-align:center'");
  puts("|+ <inttypes.h> で定義されているマクロの値(一例)");
  puts("|-");
  puts("! \ !! *8 !! *16 !! *32 !! *64 !! *LEAST8 !! *LEAST16 !! *LEAST32 !! *LEAST64 !! *FAST8 !! *FAST16 !! *FAST32 !! *FAST64 !! *MAX !! *PTR");
  puts("|-");
  puts("! PRId*");
  puts("| " PRId8 " || " PRId16 " || " PRId32 " || " PRId64 " || " PRIdLEAST8 " || " PRIdLEAST16 " || " PRIdLEAST32 " || " PRIdLEAST64 " || " PRIdFAST8 " || " PRIdFAST16 " || " PRIdFAST32 " || " PRIdFAST64 " || " PRIdMAX " || " PRIdPTR "");
  puts("|-");
  puts("! PRIi*");
  puts("| " PRIi8 " || " PRIi16 " || " PRIi32 " || " PRIi64 " || " PRIiLEAST8 " || " PRIiLEAST16 " || " PRIiLEAST32 " || " PRIiLEAST64 " || " PRIiFAST8 " || " PRIiFAST16 " || " PRIiFAST32 " || " PRIiFAST64 " || " PRIiMAX " || " PRIiPTR "");
  puts("|-");
  puts("! PRIo*");
  puts("| " PRIo8 " || " PRIo16 " || " PRIo32 " || " PRIo64 " || " PRIoLEAST8 " || " PRIoLEAST16 " || " PRIoLEAST32 " || " PRIoLEAST64 " || " PRIoFAST8 " || " PRIoFAST16 " || " PRIoFAST32 " || " PRIoFAST64 " || " PRIoMAX " || " PRIoPTR "");
  puts("|-");
  puts("! PRIu*");
  puts("| " PRIu8 " || " PRIu16 " || " PRIu32 " || " PRIu64 " || " PRIuLEAST8 " || " PRIuLEAST16 " || " PRIuLEAST32 " || " PRIuLEAST64 " || " PRIuFAST8 " || " PRIuFAST16 " || " PRIuFAST32 " || " PRIuFAST64 " || " PRIuMAX " || " PRIuPTR "");
  puts("|-");
  puts("! PRIx*");
  puts("| " PRIx8 " || " PRIx16 " || " PRIx32 " || " PRIx64 " || " PRIxLEAST8 " || " PRIxLEAST16 " || " PRIxLEAST32 " || " PRIxLEAST64 " || " PRIxFAST8 " || " PRIxFAST16 " || " PRIxFAST32 " || " PRIxFAST64 " || " PRIxMAX " || " PRIxPTR "");
  puts("|-");
  puts("! PRIX*");
  puts("| " PRIX8 " || " PRIX16 " || " PRIX32 " || " PRIX64 " || " PRIXLEAST8 " || " PRIXLEAST16 " || " PRIXLEAST32 " || " PRIXLEAST64 " || " PRIXFAST8 " || " PRIXFAST16 " || " PRIXFAST32 " || " PRIXFAST64 " || " PRIXMAX " || " PRIXPTR "");
  puts("|-");
  puts("! SCNd*");
  puts("| " SCNd8 " || " SCNd16 " || " SCNd32 " || " SCNd64 " || " SCNdLEAST8 " || " SCNdLEAST16 " || " SCNdLEAST32 " || " SCNdLEAST64 " || " SCNdFAST8 " || " SCNdFAST16 " || " SCNdFAST32 " || " SCNdFAST64 " || " SCNdMAX " || " SCNdPTR "");
  puts("|-");
  puts("! SCNi*");
  puts("| " SCNi8 " || " SCNi16 " || " SCNi32 " || " SCNi64 " || " SCNiLEAST8 " || " SCNiLEAST16 " || " SCNiLEAST32 " || " SCNiLEAST64 " || " SCNiFAST8 " || " SCNiFAST16 " || " SCNiFAST32 " || " SCNiFAST64 " || " SCNiMAX " || " SCNiPTR "");
  puts("|-");
  puts("! SCNo*");
  puts("| " SCNo8 " || " SCNo16 " || " SCNo32 " || " SCNo64 " || " SCNoLEAST8 " || " SCNoLEAST16 " || " SCNoLEAST32 " || " SCNoLEAST64 " || " SCNoFAST8 " || " SCNoFAST16 " || " SCNoFAST32 " || " SCNoFAST64 " || " SCNoMAX " || " SCNoPTR "");
  puts("|-");
  puts("! SCNu*");
  puts("| " SCNu8 " || " SCNu16 " || " SCNu32 " || " SCNu64 " || " SCNuLEAST8 " || " SCNuLEAST16 " || " SCNuLEAST32 " || " SCNuLEAST64 " || " SCNuFAST8 " || " SCNuFAST16 " || " SCNuFAST32 " || " SCNuFAST64 " || " SCNuMAX " || " SCNuPTR "");
  puts("|-");
  puts("! SCNx*");
  puts("| " SCNx8 " || " SCNx16 " || " SCNx32 " || " SCNx64 " || " SCNxLEAST8 " || " SCNxLEAST16 " || " SCNxLEAST32 " || " SCNxLEAST64 " || " SCNxFAST8 " || " SCNxFAST16 " || " SCNxFAST32 " || " SCNxFAST64 " || " SCNxMAX " || " SCNxPTR "");
  puts("");
  puts("|}");
}
実行結果
{| class='wikitable' style='text-align:center'
|+ <inttypes.h> で定義されているマクロの値(一例)
|-
! \ !! *8 !! *16 !! *32 !! *64 !! *LEAST8 !! *LEAST16 !! *LEAST32 !! *LEAST64 !! *FAST8 !! *FAST16 !! *FAST32 !! *FAST64 !! *MAX !! *PTR
|-
! PRId*
| d || d || d || ld || d || d || d || ld || d || ld || ld || ld || ld || ld
|-
! PRIi*
| i || i || i || li || i || i || i || li || i || li || li || li || li || li
|-
! PRIo*
| o || o || o || lo || o || o || o || lo || o || lo || lo || lo || lo || lo
|-
! PRIu*
| u || u || u || lu || u || u || u || lu || u || lu || lu || lu || lu || lu
|-
! PRIx*
| x || x || x || lx || x || x || x || lx || x || lx || lx || lx || lx || lx
|-
! PRIX*
| X || X || X || lX || X || X || X || lX || X || lX || lX || lX || lX || lX
|-
! SCNd*
| hhd || hd || d || ld || hhd || hd || d || ld || hhd || ld || ld || ld || ld || ld
|-
! SCNi*
| hhi || hi || i || li || hhi || hi || i || li || hhi || li || li || li || li || li
|-
! SCNo*
| hho || ho || o || lo || hho || ho || o || lo || hho || lo || lo || lo || lo || lo
|-
! SCNu*
| hhu || hu || u || lu || hhu || hu || u || lu || hhu || lu || lu || lu || lu || lu
|-
! SCNx*
| hhx || hx || x || lx || hhx || hx || x || lx || hhx || lx || lx || lx || lx || lx

|}
参考
COLS = %w(8 16 32 64 LEAST8 LEAST16 LEAST32 LEAST64 FAST8 FAST16 FAST32 FAST64 MAX PTR)
ROWS = %w(PRId PRIi PRIo PRIu PRIx PRIX SCNd SCNi SCNo SCNu SCNx)

puts <<EOS
#include <stdio.h>
#include <inttypes.h>

int main(void) {
#{
(<<EOS1
{| class='wikitable' style='text-align:center'
|+ <inttypes.h> で定義されているマクロの値(一例)
|-
! \ !! #{COLS.map{|col| "*#{col}" }.join(" !! ")}
#{ ROWS.map{|row|
<<EOS2
|-
! #{row}*
| #{COLS.map{|col| %|" #{row}#{col} "| }.join(" || ")}
EOS2
}.join()
}
|}
EOS1
).split(/\n/).map{|line| %|  puts("#{line}");| }.join("\n")
}
}
EOS

歴史 編集

<inttypes.h> は、ISO/IEC 9899:1999 (C99)で追加されました[6]

脚註 編集

  1. ^ C11: WG14/N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x. ISO/IEC. p. 217, §7.8 Format conversion of integer types. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf. 
  2. ^ C11: WG14/N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x. ISO/IEC. p. 217, §7.8.1 Macros for format specifiers. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf. 
  3. ^ C11: WG14/N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x. ISO/IEC. p. 218, §7.8.2 Functions for greatest-width integer types. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf. 
  4. ^ N2176 C17 ballot ISO/IEC 9899:2017. ISO/IEC JTC1/SC22/WG14. p. 159, §7.8.2.1 The imaxabs function. オリジナルの2018-12-30時点によるアーカイブ。. https://web.archive.org/web/20181230041359/http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf. 
  5. ^ N2176 C17 ballot ISO/IEC 9899:2017. ISO/IEC JTC1/SC22/WG14. p. 159, §7.8.2.2 The imaxdiv function. オリジナルの2018-12-30時点によるアーカイブ。. https://web.archive.org/web/20181230041359/http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf. 
  6. ^ C11: WG14/N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x. ISO/IEC. p. xv, § ForewordMajor changes in the second edition included:. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf. 

参考文献 編集