「C言語/標準ライブラリ/inttypes.h」の版間の差分

出典: フリー教科書『ウィキブックス(Wikibooks)』
削除された内容 追加された内容
Ef3 (トーク | 投稿記録)
タグ: 2017年版ソースエディター
Ef3 (トーク | 投稿記録)
{{Nav}}、Cat.
タグ: 2017年版ソースエディター
1 行 1 行
{{Nav}}
ヘッダー <code><inttypes.h></code> は、ヘッダー <code><stdint.h></code> をインクルードしホストされた実装によって提供される追加機能で <code><stdint.h></code> を拡張します<ref name="jtc1-sc22-wg14-n1570-7.8">{{cite book

ヘッダーファイル <code><inttypes.h></code> は、ヘッダー <code><stdint.h></code> をインクルードしホストされた実装によって提供される追加機能で <code><stdint.h></code> を拡張します<ref name="jtc1-sc22-wg14-n1570-7.8">{{cite book
| url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
| url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
| title= C11: WG14/N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x
| title= C11: WG14/N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x
91 行 93 行
* 国際標準化機構/国際電気標準会議 [https://www.iso.org/obp/ui/#iso:std:iso-iec:9899:ed-4:v1:en ISO/IEC 9899:2018(en) Information technology — Programming languages — C](2018-07-05)
* 国際標準化機構/国際電気標準会議 [https://www.iso.org/obp/ui/#iso:std:iso-iec:9899:ed-4:v1:en ISO/IEC 9899:2018(en) Information technology — Programming languages — C](2018-07-05)


[[Category:C言語|あらめんと]]
[[Category:C言語|すうかたのくせい]]

2021年8月18日 (水) 03:33時点における版

ヘッダーファイル <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);

歴史

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

脚註

  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. ^ 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. 

参考文献