.\" Copyright (c) 1999 Andries Brouwer (aeb@cwi.nl) .\" .\" Earlier versions of this page influenced the present text. .\" It was derived from a Berkeley page with version .\" @(#)printf.3 6.14 (Berkeley) 7/30/91 .\" converted for Linux by faith@cs.unc.edu, updated by .\" Helmut.Geyer@iwr.uni-heidelberg.de, agulbra@troll.no and Bruno Haible. .\" .\" %%%LICENSE_START(GPLv2+_DOC_FULL) .\" This is free documentation; you can redistribute it and/or .\" modify it under the terms of the GNU General Public License as .\" published by the Free Software Foundation; either version 2 of .\" the License, or (at your option) any later version. .\" .\" The GNU General Public License's references to "object code" .\" and "executables" are to be interpreted as the output of any .\" document formatting or typesetting system, including .\" intermediate and printed output. .\" .\" This manual is distributed in the hope that it will be useful, .\" but WITHOUT ANY WARRANTY; without even the implied warranty of .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the .\" GNU General Public License for more details. .\" .\" You should have received a copy of the GNU General Public .\" License along with this manual; if not, see .\" . .\" %%%LICENSE_END .\" .\" 1999-11-25 aeb - Rewritten, using SUSv2 and C99. .\" 2000-07-26 jsm28@hermes.cam.ac.uk - three small fixes .\" 2000-10-16 jsm28@hermes.cam.ac.uk - more fixes .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1997 YOSHINO Takashi all rights reserved. .\" Translated 1998-02-17, YOSHINO Takashi .\" Updated 2000-10-02, Kentaro Shirakata .\" Updated 2001-01-29, Kentaro Shirakata .\" Updated 2002-01-03, Kentaro Shirakata .\" Updated 2002-10-17, Kentaro Shirakata .\" Updated 2005-03-15, Akihiro MOTOKI .\" Updated 2006-07-20, Akihiro MOTOKI .\" Updated 2008-02-10, Akihiro MOTOKI , LDP v2.77 .\" Updated 2009-03-03, Akihiro MOTOKI , LDP v3.19 .\" Updated 2012-05-29, Akihiro MOTOKI .\" Updated 2013-05-06, Akihiro MOTOKI .\" Updated 2013-07-22, Akihiro MOTOKI .\" .TH PRINTF 3 2020\-11\-01 GNU "Linux Programmer's Manual" .SH 名前 printf, fprintf, dprintf, sprintf, snprintf, vprintf, vfprintf, vdprintf, vsprintf, vsnprintf \- 指定された書式に変換して出力を行う .SH 書式 .nf \fB#include \fP .PP \fBint printf(const char *\fP\fIformat\fP\fB, ...);\fP \fBint fprintf(FILE *\fP\fIstream\fP\fB, const char *\fP\fIformat\fP\fB, ...);\fP \fBint dprintf(int \fP\fIfd\fP\fB, const char *\fP\fIformat\fP\fB, ...);\fP \fBint sprintf(char *\fP\fIstr\fP\fB, const char *\fP\fIformat\fP\fB, ...);\fP \fBint snprintf(char *\fP\fIstr\fP\fB, size_t \fP\fIsize\fP\fB, const char *\fP\fIformat\fP\fB, ...);\fP \fB#include \fP .PP \fBint vprintf(const char *\fP\fIformat\fP\fB, va_list \fP\fIap\fP\fB);\fP \fBint vfprintf(FILE *\fP\fIstream\fP\fB, const char *\fP\fIformat\fP\fB, va_list \fP\fIap\fP\fB);\fP \fBint vdprintf(int \fP\fIfd\fP\fB, const char *\fP\fIformat\fP\fB, va_list \fP\fIap\fP\fB);\fP \fBint vsprintf(char *\fP\fIstr\fP\fB, const char *\fP\fIformat\fP\fB, va_list \fP\fIap\fP\fB);\fP \fBint vsnprintf(char *\fP\fIstr\fP\fB, size_t \fP\fIsize\fP\fB, const char *\fP\fIformat\fP\fB, va_list \fP\fIap\fP\fB);\fP .fi .PP .RS -4 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照): .RE .PP .ad l \fBsnprintf\fP(), \fBvsnprintf\fP(): .RS 4 _XOPEN_SOURCE\ >=\ 500 || _ISOC99_SOURCE || || /* Glibc versions <= 2.19: */ _BSD_SOURCE .RE .PP \fBdprintf\fP(), \fBvdprintf\fP(): .PD 0 .RS 4 .TP 4 glibc 2.10 以降: _POSIX_C_SOURCE\ >=\ 200809L .TP glibc 2.10 より前: _GNU_SOURCE .RE .ad .PD .SH 説明 \fBprintf\fP() 関数グループは、以下で述べるように、 \fIformat\fP に従って出力を生成するものである。 \fBprintf\fP() と \fBvprintf\fP() は出力を \fIstdout\fP (標準出力ストリーム) に書き出す。 \fBfprintf\fP() と \fBvfprintf\fP() は出力を指定された出力 \fIstream\fP に書き出す。 \fBsprintf\fP(), \fBsnprintf\fP(), \fBvsprintf\fP(), \fBvsnprintf\fP() は出力を文字列 \fIstr\fP に書き込む。 .PP \fBdprintf\fP() 関数は \fBfprintf\fP(3) 関数と同じだが、 この関数は \fIstdio\fP ストリームではなくファイルディスクリプター \fIfd\fP に対して出力を行う点が異なる。 .PP \fBsnprintf\fP() と \fBvsnprintf\fP() は最大で \fIsize\fP バイトを \fIstr\fP に書き込む (\fIsize\fP には文字列を終端するヌルバイト (\(aq\e0\(aq) もを含まれる)。 .PP \fBvprintf\fP(), \fBvfprintf\fP(), \fBvdprintf\fP(), \fBvsprintf\fP(), \fBvsnprintf\fP() の各関数はそれぞれ \fBprintf\fP(), \fBfprintf\fP(), \fBdprintf\fP(), \fBsprintf\fP(), \fBsnprintf\fP(), の各関数と等価であり、可変数引数の代わりに \fIva_list\fP を引数として呼び出される点だけが異なる。 これらの関数では \fIva_end\fP マクロは呼び出されない。 これらの関数は \fIva_arg\fP を呼び出すので、呼び出し後の \fIap\fP の値は未定義である。 \fBstdarg\fP(3) を参照のこと。 .PP これらの関数はすべて \fIformat\fP 文字列の制御に従って出力を書き出す。 \fIformat\fP 文字列は、これに続く引数 (または \fBstdarg\fP(3) の可変長引数機構を使ってアクセスできる引数) をどのように変換して出力するかを指定する。 .PP C99 と POSIX.1\-2001 では、 \fBsprintf\fP(), \fBsnprintf\fP(), \fBvsprintf\fP(), \fBvsnprintf\fP() の呼び出しで、範囲が重複するオブジェクト間でコピーが発生する場合の 結果は不定であると規定されている (例えば、出力先の文字列と入力された 引数の一つが同じバッファーを参照している場合などである)。 「注意」の節を参照。 .SS フォーマット文字列のフォーマット フォーマット文字列は文字の列で、 (もしあるなら) 初期シフト状態で始まり、初期シフト状態で終わる。 フォーマット用の文字列は 0 個以上の命令 (directives) によって構成される。 命令には、通常文字と変換指定 (conversion specifications) がある。 通常文字は \fB%\fP 以外の文字で、出力ストリームにそのままコピーされる。 変換指定は、それぞれが 0 個以上の引数を取る。 各変換指定は文字 \fB%\fP で始まり、 \fI変換指定子 (conversion specifier)\fP で終わる。 \fB%\fP と変換指定子の間には、0 個以上の \fIフラグ 、\fP 最小 \fIフィールド幅 、\fP \fI精度 、\fP \fI長さ修飾子\fP を (この順序で) 置くことができる。 .PP The arguments must correspond properly (after type promotion) with the conversion specifier. By default, the arguments are used in the order given, where each \(aq*\(aq (see \fIField width\fP and \fIPrecision\fP below) and each conversion specifier asks for the next argument (and it is an error if insufficiently many arguments are given). One can also specify explicitly which argument is taken, at each place where an argument is required, by writing "%m$" instead of \(aq%\(aq and "*m$" instead of \(aq*\(aq, where the decimal integer \fIm\fP denotes the position in the argument list of the desired argument, indexed starting from 1. Thus, .PP .in +4n .EX printf("%*d", width, num); .EE .in .PP と .PP .in +4n .EX printf("%2$*1$d", width, num); .EE .in .PP は等価である。 二番目の書き方では同じ引数を繰り返し参照することができる。 C99 標準には、 Single UNIX Specification 由来の \(aq$\(aq を使った書き方は含まれていない。 \(aq$\(aq を使ったスタイルを使うと、引数を取る変換及び幅と精度の引数を 全てこのスタイルで指定しなければならないが、 引数を消費しない "%%" フォーマットと混ざっているかもしれない。 \(aq$\(aq で指定される引数の番号に空きがあってはならない。 例えば、もし引数 1 と 3 が指定されると、引数 2 もフォーマット文字列のどこかで 指定されなければならない。 .PP 数値変換には小数点や 1000 単位の区切り文字を使うものもある。 実際にどの文字を使うかはロケールの \fBLC_NUMERIC\fP による (\fBsetlocale\fP(3) 参照)。 POSIX ロケールでは小数点に \(aq.\(aq を用い、 区切り文字は使わない。 従って、 .PP .in +4n .EX printf("%\(aq.2f", 1234567.89); .EE .in .PP は、 POSIX ロケールでは "1234567.89" 、 nl_NL ロケールでは "1234567,89"、 da_DK ロケールでは "1.234.567,89" となる。 .SS フラグ文字 % 文字の後ろには 0 個以上のフラグ文字が続く。 .TP \fB#\fP 値は「別の形式」に変換される。 \fBo\fP 変換の場合、(先頭文字が 0 になっていない場合に先頭に 0 を追加することで) 出力文字列の最初の文字を 0 にする。 \fBx\fP と \fBX\fP 変換の場合、数値が 0 でないときには文字列 "0x" (\fBX\fP 変換の場合には "0X") が前に付与される。 \fBa\fP, \fBA\fP, \fBe\fP, \fBE\fP, \fBf\fP, \fBF\fP, \fBg\fP, \fBG\fP 変換では、 小数点に続く数字がなくても、 出力には常に小数点が含まれる (通常は、小数点の後に数字が続く場合にのみ、 小数点が表示される)。 \fBg\fP と \fBG\fP 変換の場合、他の変換とは異なり、末尾のゼロが変換結果から削除されない。 その他の変換では、結果は未定義である。 .TP \fB\&0\fP 値をゼロで埋める。 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, \fBX\fP, \fBa\fP, \fBA\fP, \fBe\fP, \fBE\fP, \fBf\fP, \fBF\fP, \fBg\fP, \fBG\fP 変換では、変換した値の左側を空白文字の代わりにゼロで埋める。 \fB\&0\fP と \fB\-\fP が両方とも指定された場合は、 \fB\&0\fP フラグは無視される。 精度が数値変換 (\fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, \fBX\fP) と同時に指定された場合には、 \fB\&0\fP フラグは無視される。 その他の変換では、動作は未定義である。 .TP \fB\-\fP 変換値をフィールド境界で左揃えにする (デフォルトは右揃えである)。 変換された値は 左側ではなく右側を空白文字やゼロで埋められる。 \fB\-\fP と \fB\&0\fP の両方が指定された場合には、 \fB\-\fP が優先される。 .TP \&\fB' '\fP (1個の半角スペース) 符号付き変換で生成された正の数字の前に空白 (または空文字列) が置かれる。 .TP \fB+\fP 符号付き変換によって出力される数字の前に、常に符号 (+ か \-) が置かれる。 デフォルトでは、符号は負の数字の場合のみ付与される。 \fB+\fP と半角スペースの 両方が使われている場合には、 \fB+\fP が優先される。 .PP 上記の 5 つのフラグは C99 標準で定義されている。 Single UNIX Specified では、さらにもう一つフラグ文字が規定されている。 .TP \fB\(aq\fP 10進数変換 (\fBi\fP, \fBd\fP, \fBu\fP, \fBf\fP, \fBF\fP, \fBg\fP, \fBG\fP) において、ロケール情報に指定があれば 1000 単位の区切り文字を出力する (\fBsetlocale\fP(3) 参照)。 \fBgcc\fP(1) の多くのバージョンは、このオプションを解釈することができず、 警告を出力することに注意せよ。 (\fI%\(aqF\fP は SUSv2 には含まれていなかったが、 SUSv3 で追加された。 .PP glibc 2.2 では、さらに一つフラグ文字が追加されている。 .TP \fBI\fP .\" outdigits keyword in locale file 10進整数変換 (\fBi\fP, \fBd\fP, \fBu\fP) において、ロケールの代替出力数字があれば、それを用いて出力する。 例えば、 glibc 2.2.3 以降では、ペルシア ("fa_IR") ロケールで アラビア数字 (Arabic\-Indic digits) を出力できる。 .SS フィールド幅 最小のフィールド幅を指定する 10進数の数値文字列 (文字列の最初の文字は ゼロ以外)。本項目はオプションである。 変換された値の文字数がフィールド長よりも少ない場合、 フィールドの左側をスペースで埋める (左揃えのフラグがある場合は右側を埋める)。 10進数の文字列の代わりに "*" や "*m$" (\fIm\fP は 10進整数) を書くこともできる。 "*" と "*m$" はそれぞれ、次の引数と \fIm\fP 番目の引数をフィールド幅として 使うことを指定する (これらの引数は \fIint\fP 型でなければならない)。 フィールド幅に負の数が指定された場合は、 \(aq\-\(aq フラグと正の数のフィールド幅として扱われる。 フィールド幅が小さかったり指定がなかったりしても、フィールドが切り詰められる ことはない。もし変換結果がフィールド幅よりも広かった場合、 フィールドは変換結果が入る幅に広げられる。 .SS 精度 オプションである精度は、ピリオド (\(aq.\(aq) とそれに続く10進数という 形式で指定する (10進数はオプション) 。 10進数の文字列の代わりに "*" や "*m$" (\fIm\fP は 10 進整数)を書くこともできる。 "*" と "*m$" はそれぞれ、次の引数と \fIm\fP 番目の引数を精度として 使うことを指定する (これらの引数は \fIint\fP 型でなければならない)。 精度として \(aq.\(aq だけが指定された場合、 精度はゼロとみなされる。 精度が負の数だった場合、 精度は指定されなかったものとみなされる。 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, \fBX\fP 変換では、表示される最小の桁数を指定する。 \fBa\fP, \fBA\fP, \fBe\fP, \fBE\fP, \fBf\fP, \fBF\fP 変換では、小数点以下に表示される数字の桁数を指定する。 \fBg\fP と \fBG\fP 変換では、有効数字の最大桁数を指定する。 \fBs\fP と \fBS\fP 変換では、文字列から出力される最大文字数を指定する。 .SS 長さ修飾子 「整数変換」とは、 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, \fBX\fP 変換のことである。 .TP \fBhh\fP 整数変換に対応する引数が \fIsigned char\fP か \fIunsigned char\fP で、 \fBn\fP 変換に対応する引数が \fIsigned char\fP へのポインターであることを示す。 .TP \fBh\fP 整数変換に対応する引数が \fIshort\fP か \fIunsigned short\fP で、 \fBn\fP 変換に対応する引数が \fIshort\fP へのポインターであることを示す。 .TP \fBl\fP 各変換に対応する引数が、 整数変換では \fIlong\fP か \fIunsigned long\fP、 \fBn\fP 変換では \fIlong\fP へのポインター、 \fBc\fP 変換では \fIwint_t\fP、 \fBs\fP 変換では \fIwchar_t\fP へのポインターであることを示す。 .TP \fBll\fP (エルエル) 整数変換に対応する引数が \fIlong long\fP か \fIunsigned long long\fP で、 \fBn\fP 変換に対応する引数が \fIlong long\fP へのポインターであることを示す。 .TP \fBq\fP A synonym for \fBll\fP. This is a nonstandard extension, derived from BSD; avoid its use in new code. .TP \fBL\fP \fBa\fP, \fBA\fP, \fBe\fP, \fBE\fP, \fBf\fP, \fBF\fP, \fBg\fP, \fBG\fP 変換に対応する引数が \fIlong double\fP であることを示す。 (C99 では %LF を使うことを認めているが、SUSv2 では認められていない。) .TP \fBj\fP 整数変換に対応する引数が \fIintmax_t\fP か \fIuintmax_t\fP で、 \fBn\fP 変換に対応する引数が \fIintmax_t\fP へのポインターであることを示す。 .TP \fBz\fP 整数変換に対応する引数が \fIsize_t\fP か \fIssize_t\fP で、 \fBn\fP 変換に対応する引数が \fIsize_t\fP へのポインターであることを示す。 .TP \fBZ\fP A nonstandard synonym for \fBz\fP that predates the appearance of \fBz\fP. Do not use in new code. .TP \fBt\fP 整数変換に対応する引数が \fIptrdiff_t\fP で、 \fBn\fP 変換に対応する引数が \fIptrdiff_t\fP へのポインターであることを示す。 .PP SUSv3 specifies all of the above, except for those modifiers explicitly noted as being nonstandard extensions. SUSv2 specified only the length modifiers \fBh\fP (in \fBhd\fP, \fBhi\fP, \fBho\fP, \fBhx\fP, \fBhX\fP, \fBhn\fP) and \fBl\fP (in \fBld\fP, \fBli\fP, \fBlo\fP, \fBlx\fP, \fBlX\fP, \fBln\fP, \fBlc\fP, \fBls\fP) and \fBL\fP (in \fBLe\fP, \fBLE\fP, \fBLf\fP, \fBLg\fP, \fBLG\fP). .PP .\" As a nonstandard extension, the GNU implementations treats \fBll\fP and \fBL\fP as synonyms, so that one can, for example, write \fBllg\fP (as a synonym for the standards\-compliant \fBLg\fP) and \fBLd\fP (as a synonym for the standards compliant \fBlld\fP). Such usage is nonportable. .SS 変換指定子 適用される変換の型を指定する文字。 変換指定子とその意味は以下の通りである。 .TP \fBd\fP, \fBi\fP \fIint\fP 引数を符号付き 10 進表記に変換する。 精度指定があれば、精度で指定した桁数は必ず出力される。変換後の値が 指定された桁数に足りない場合は、左側が 0 で埋められる。 デフォルトの精度は 1 である。 0 を表示しようとした時に、明示的に精度として 0 が指定されていると、 出力は空文字列となる。 .TP \fBo\fP, \fBu\fP, \fBx\fP, \fBX\fP \fIunsigned int\fP 引数を、 符号なし8進数 (\fBo\fP), 符号なし10進数 (\fBu\fP), 符号なし16進数 (\fBx\fP と \fBX\fP) に変換する。 \fBx\fP 変換では \fBabcdef\fP が使用され、 \fBX\fP 変換では \fBABCDEF\fP が使用される。 精度指定があれば、精度で指定した桁数は必ず出力される。変換後の値が 指定された桁数に足りない場合は、左側が 0 で埋められる。 .TP \fBe\fP, \fBE\fP The \fIdouble\fP argument is rounded and converted in the style [\-]d\fB\&.\fPddd\fBe\fP\(+-dd where there is one digit (which is nonzero if the argument is nonzero) before the decimal\-point character and the number of digits after it is equal to the precision; if the precision is missing, it is taken as 6; if the precision is zero, no decimal\-point character appears. An \fBE\fP conversion uses the letter \fBE\fP (rather than \fBe\fP) to introduce the exponent. The exponent always contains at least two digits; if the value is zero, the exponent is 00. .TP \fBf\fP, \fBF\fP \fIdouble\fP 引数を丸めて [\-]ddd\fB\&.\fPddd の形の10進表現に変換する。 小数点の後の桁数は、精度で指定された値となる。 精度が指定されていない場合には 6 として扱われる。 精度として明示的に 0 が指定されたときには、小数点以下は表示されない。 小数点を表示する際には、小数点の前に少なくとも一桁は数字が表示される。 .IP (SUSv2 では、\fBF\fP は規定されておらず、無限や NaN に関する文字列表現を行ってもよいことになっている。 SUSv3 では \fBF\fP の規定が追加された。 C99 標準では、\fBf\fP 変換では、無限は "[\-]inf" か "[\-]infinity" と表示し、 NaN は文字列の先頭に `nan' をつけて表示するように規定されている。 \fBF\fP 変換の場合は "[\-]INF", "[\-]INFINITY", "NAN" と表示される。) .TP \fBg\fP, \fBG\fP \fIdouble\fP 引数を \fBf\fP か \fBe\fP (\fBG\fP 変換の場合は \fBF\fP か \fBE\fP) の形式に変換する。 精度は表示する桁数を指定する。 精度が指定されない場合は、6桁とみなされる。 精度が 0 の場合は、1桁とみなされる。 変換される値の指数が、 \-4 より小さいか、精度以上の場合に、 \fBe\fP 形式が使用される。 変換された結果の小数部分の末尾の 0 は削除される。小数点が表示されるのは、 小数点以下に数字が少なくとも一つある場合にだけである。 .TP \fBa\fP, \fBA\fP (C99; not in SUSv2, but added in SUSv3) For \fBa\fP conversion, the \fIdouble\fP argument is converted to hexadecimal notation (using the letters abcdef) in the style [\-]\fB0x\fPh\fB\&.\fPhhhh\fBp\fP\(+-d; for \fBA\fP conversion the prefix \fB0X\fP, the letters ABCDEF, and the exponent separator \fBP\fP is used. There is one hexadecimal digit before the decimal point, and the number of digits after it is equal to the precision. The default precision suffices for an exact representation of the value if an exact representation in base 2 exists and otherwise is sufficiently large to distinguish values of type \fIdouble\fP. The digit before the decimal point is unspecified for nonnormalized numbers, and nonzero but otherwise unspecified for normalized numbers. The exponent always contains at least one digit; if the value is zero, the exponent is 0. .TP \fBc\fP \fBl\fP 修飾子がなければ、 \fIint\fP 引数を \fIunsigned char\fP に変換して、その結果に対応する文字を出力する。 \fBl\fP 修飾子があれば、 \fIwint_t\fP (ワイド文字) 引数を、 \fBwcrtomb\fP(3) 関数を初期シフト状態で呼び出してマルチバイト文字列に変換し、 変換されたマルチバイト文字列を出力する。 .TP \fBs\fP \fBl\fP 修飾子がない場合、 引数は \fIconst char\ *\fP 型で文字型の配列へのポインター (文字列へのポインター) であることが 期待されている。配列中の文字は、終端の ヌルバイト (\(aq\e0\(aq) が出てくるまで出力される (終端文字は出力されない)。 精度が指定されていると、指定された字数以上は出力されない。 精度が指定された場合には、終端バイトが存在する必要はない。 精度が指定されていなかったり、精度の値が配列の大きさより大きい場合には、 配列は終端のヌルバイトを含んでいなければならない。 .IP \fBl\fP 修飾子が指定されている場合、 引数は \fIconst wchar_t\ *\fP 型でワイド文字の配列へのポインターであることが期待されている。 配列中のワイド文字は (1文字毎に \fBwcrtomb\fP(3) を呼び出して) マルチバイト文字に変換される (最初のワイド文字の変換の前に \fBwcrtomb\fP() のシフト状態を初期状態に戻してから変換は行われる)。 マルチバイト文字への変換は、文字列を終端するヌルワイド文字が 出てくるまで行われ、終端ヌルワイド文字も含めて変換される。 結果のマルチバイト文字列は、終端のヌルバイトが出てくるまで 出力される (終端のヌルバイトは出力されない)。 精度が指定された場合、指定されたバイト数以上には出力されない。 但し、マルチバイト文字の一部分だけが出力されることはない。 精度は「バイト」数を指定するものであり、「ワイド文字」数や 「画面での位置」を指定するものではないことに注意。 精度が指定されていて、さらに出力が配列の末尾に達する前に出力バイト数が 精度の値を超える場合だけは、配列はヌルワイド文字で終端されていなくてもよい。 それ以外の場合は、必ず配列はヌルワイド文字で終端されていなければならない。 .TP \fBC\fP (C99, C11 にはないが SUSv2, SUSv3, SUSv4 にはある) \fBlc\fP と同じ。使ってはならない。 .TP \fBS\fP (C99, C11 にはないが SUSv2, SUSv3, SUSv4 にはある) \fBls\fP と同じ。使ってはならない。 .TP \fBp\fP \fIvoid\ *\fP ポインター引数を (\fB%#x\fP や \fB%#lx\fP のような) 16 進数で出力する。 .TP \fBn\fP The number of characters written so far is stored into the integer pointed to by the corresponding argument. That argument shall be an \fIint\ *\fP, or variant whose size matches the (optionally) supplied integer length modifier. No argument is converted. (This specifier is not supported by the bionic C library.) The behavior is undefined if the conversion specification includes any flags, a field width, or a precision. .TP \fBm\fP (glibc での拡張; uClibc と musl で対応) \fIstrerror(errno)\fP の出力を表示する。引数は必要ない。 .TP \fB%\fP \(aq%\(aq 文字を出力する。変換される引数は無い。 変換指定全体を書くと "%%" となる。 .SH 返り値 成功時には、上記の関数は書き込まれた文字数を返す (文字列の最後を示すために使用するヌルバイトは数に含まれない)。 .PP \fBsnprintf\fP() と \fBvsnprintf\fP() は、 \fIsize\fP バイトを越える文字数を書き込まない (\fIsize\fP には文字列を終端するヌルバイト (\(aq\e0\(aq) も含まれる)。 この制限によって出力が切り詰められた場合には、 もし十分なスペースがあれば書き込まれたであろう文字の個数 (文字列を終端するヌルバイトを除く) を返す。 従って、返り値が \fIsize\fP 以上だった場合、出力が切り詰められたことを意味する (後述の注意も参照のこと)。 .PP エラーが発生した場合は、負の数を返す。 .SH 属性 この節で使用されている用語の説明については、 \fBattributes\fP(7) を参照。 .TS allbox; lbw23 lb lb l l l. インターフェース 属性 値 T{ \fBprintf\fP(), \fBfprintf\fP(), .br \fBsprintf\fP(), \fBsnprintf\fP(), .br \fBvprintf\fP(), \fBvfprintf\fP(), .br \fBvsprintf\fP(), \fBvsnprintf\fP() T} Thread safety MT\-Safe locale .TE .sp 1 .SH 準拠 \fBfprintf\fP(), \fBprintf\fP(), \fBsprintf\fP(), \fBvprintf\fP(), \fBvfprintf\fP(), \fBvsprintf\fP(): POSIX.1\-2001, POSIX.1\-2008, C89, C99. .PP \fBsnprintf\fP(), \fBvsnprintf\fP(): POSIX.1\-2001, POSIX.1\-2008, C99. .PP \fBdprintf\fP() と \fBvdprintf\fP() は、どちらも元は GNU による拡張であったが、 POSIX.1\-2008 で標準化された。 .PP .\" .PP .\" Linux libc4 knows about the five C standard flags. .\" It knows about the length modifiers \fBh\fP, \fBl\fP, \fBL\fP, .\" and the conversions .\" \fBc\fP, \fBd\fP, \fBe\fP, \fBE\fP, \fBf\fP, \fBF\fP, .\" \fBg\fP, \fBG\fP, \fBi\fP, \fBn\fP, \fBo\fP, \fBp\fP, .\" \fBs\fP, \fBu\fP, \fBx\fP, and \fBX\fP, .\" where \fBF\fP is a synonym for \fBf\fP. .\" Additionally, it accepts \fBD\fP, \fBO\fP, and \fBU\fP as synonyms .\" for \fBld\fP, \fBlo\fP, and \fBlu\fP. .\" (This is bad, and caused serious bugs later, when .\" support for \fB%D\fP disappeared.) .\" No locale-dependent radix character, .\" no thousands' separator, no NaN or infinity, no "%m$" and "*m$". .\" .PP .\" Linux libc5 knows about the five C standard flags and the \(aq flag, .\" locale, "%m$" and "*m$". .\" It knows about the length modifiers \fBh\fP, \fBl\fP, \fBL\fP, .\" \fBZ\fP, and \fBq\fP, but accepts \fBL\fP and \fBq\fP .\" both for \fIlong double\fP and for \fIlong long\fP (this is a bug). .\" It no longer recognizes \fBF\fP, \fBD\fP, \fBO\fP, and \fBU\fP, .\" but adds the conversion character .\" .BR m , .\" which outputs .\" .IR strerror(errno) . .\" .PP .\" glibc 2.0 adds conversion characters \fBC\fP and \fBS\fP. \fBsnprintf\fP() の返り値を見ると、 SUSv2 と C99 標準は互いに矛盾している。 SUSv2 では、 \fBsnprintf\fP() が \fIsize\fP=0 で呼び出された場合、 1 未満の値を何か返り値とするように規定している。 一方 C99 では、このような場合 \fIstr\fP を NULL とし、返り値として (通常通り) 出力バッファーが十分な大きさが あった場合に出力されるであろう文字数を返す。 POSIX.1\-2001 やそれ以降では C99 の \fBsnprintf\fP() の規定にあわせたものとなっている。 .PP glibc 2.1 では、長さ修飾子 \fBhh\fP, \fBj\fP, \fBt\fP, \fBz\fP と変換文字 \fBa\fP, \fBA\fP が追加された。 .PP glibc 2.2 では、 C99 で規定された意味での変換文字 \fBF\fP と フラグ文字 \fBI\fP が追加された。 .SH 注意 テキストを \fIbuf\fP に追加するのに、軽率にも次のようなコードを使っているプログラムがある。 .PP sprintf(buf, "%s some further text", buf); .PP .\" http://sourceware.org/bugzilla/show_bug.cgi?id=7075 しかしながら、標準規格では、 \fBsprintf\fP(), \fBsnprintf\fP(), \fBvsprintf\fP(), \fBvsnprintf\fP() の呼び出しにおいて、コピー元とコピー先のバッファーが重なっていた場合の 結果は不定である、と明記されている。 使用する \fBgcc\fP(1) のバージョンや指定したコンパイラのオプション次第では、 上記のような呼び出しで、期待した結果が得られ「ない」ことがある。 .PP .\" .SH HISTORY .\" UNIX V7 defines the three routines .\" .BR printf (), .\" .BR fprintf (), .\" .BR sprintf (), .\" and has the flag \-, the width or precision *, the length modifier l, .\" and the conversions doxfegcsu, and also D,O,U,X as synonyms for ld,lo,lu,lx. .\" This is still true for 2.9.1BSD, but 2.10BSD has the flags .\" #, + and and no longer mentions D,O,U,X. .\" 2.11BSD has .\" .BR vprintf (), .\" .BR vfprintf (), .\" .BR vsprintf (), .\" and warns not to use D,O,U,X. .\" 4.3BSD Reno has the flag 0, the length modifiers h and L, .\" and the conversions n, p, E, G, X (with current meaning) .\" and deprecates D,O,U. .\" 4.4BSD introduces the functions .\" .BR snprintf () .\" and .\" .BR vsnprintf (), .\" and the length modifier q. .\" FreeBSD also has functions .\" .BR asprintf () .\" and .\" .BR vasprintf (), .\" that allocate a buffer large enough for .\" .BR sprintf (). .\" In glibc there are functions .\" .BR dprintf () .\" and .\" .BR vdprintf () .\" that print to a file descriptor instead of a stream. glibc の \fBsnprintf\fP() と \fBvsnprintf\fP() の実装は、バージョン 2.1 以降は C99 標準に準拠しており、 上記の通りの動作をする。 glibc 2.0.6 までは、出力が切り詰められた場合は \-1 を返す。 .SH バグ .\" .PP .\" Linux libc4.[45] does not have a .\" .BR snprintf (), .\" but provides a libbsd that contains an .\" .BR snprintf () .\" equivalent to .\" .BR sprintf (), .\" that is, one that ignores the .\" .I size .\" argument. .\" Thus, the use of .\" .BR snprintf () .\" with early libc4 leads to serious security problems. \fBsprintf\fP() と \fBvsprintf\fP() は勝手に十分に長い文字列領域があると仮定するので、呼び出し側は 実際の領域からあふれないように注意しなければならない。 しかし、これを保証することが不可能な場合が多い。 生成される文字列の長さはロケール依存であり、予測が難しいことに注意。 代わりに \fBsnprintf\fP() と \fBvsnprintf\fP() (または \fBasprintf\fP(3) と \fBvasprintf\fP(3)) を使うこと。 .PP .\" .PP .\" Some floating-point conversions under early libc4 .\" caused memory leaks. \fBprintf(\fP\fIfoo\fP\fB);\fP のようなコードはしばしばバグを引き起こす。 なぜなら \fIfoo\fP に % 文字が含まれてるかもしれないからである。 \fIfoo\fP が信頼できないユーザー入力から作られている場合には、 その中に \fB%n\fP が含まれていることがあり、 \fBprintf\fP() 呼び出し時にメモリーへの書き込みが起こり、 セキュリティーホールを作ることになるかもしれない。 .SH 例 \fIPi\fP を 5 桁で出力する。 .PP .in +4n .EX #include #include fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0)); .EE .in .PP 日付と時間を "Sunday, July 3, 10:02" の形式で出力する。 (\fIweekday\fP と \fImonth\fP は文字列へのポインターである) .PP .in +4n .EX #include fprintf(stdout, "%s, %s %d, %.2d:%.2d\en", weekday, month, day, hour, min); .EE .in .PP 日 \- 月 \- 年 の順序で表示を行う国も多い。 従って、国際版では書式で指定された順番で 引数を表示できなければならない。 .PP .in +4n .EX #include fprintf(stdout, format, weekday, month, day, hour, min); .EE .in .PP \fIformat\fP はロケールに依存しており、引数の順番を変えることもできる。 \fIformat\fP が .PP .in +4n .EX "%1$s, %3$d. %2$s, %4$d:%5$.2d\en" .EE .in .PP であれば、 "Sonntag, 3. Juli, 10:02" という結果になる。 .PP 十分に大きな文字列領域を確保して、そこにメッセージを格納するには (glibc 2.0 と glibc 2.1 の両方で正しく動作するコード): .PP .EX #include #include #include char * make_message(const char *fmt, ...) { int n = 0; size_t size = 0; char *p = NULL; va_list ap; /* Determine required size */ va_start(ap, fmt); n = vsnprintf(p, size, fmt, ap); va_end(ap); if (n < 0) return NULL; /* One extra byte for \(aq\e0\(aq */ size = (size_t) n + 1; p = malloc(size); if (p == NULL) return NULL; va_start(ap, fmt); n = vsnprintf(p, size, fmt, ap); va_end(ap); if (n < 0) { free(p); return NULL; } return p; } .EE .PP バージョン 2.0.6 より前の glibc で切り詰めが起こった場合、切り詰めは適切に処理されず、エラーとして扱われる。 .SH 関連項目 \fBprintf\fP(1), \fBasprintf\fP(3), \fBputs\fP(3), \fBscanf\fP(3), \fBsetlocale\fP(3), \fBstrfromd\fP(3), \fBwcrtomb\fP(3), \fBwprintf\fP(3), \fBlocale\fP(5) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は \%https://www.kernel.org/doc/man\-pages/ に書かれている。