「C++/stdio.h/出力書式」の編集履歴(バックアップ)一覧はこちら

C++/stdio.h/出力書式」(2008/03/08 (土) 16:00:00) の最新版変更点

追加された行は緑色になります。

削除された行は赤色になります。

%[フラグ][最小フィールド幅][.精度][長さ修飾子]変換指定子 #contents ---- **フラグ ***- (左寄せ) left、setf(ios::left)、setiosflags(ios::left) のいずれかを指定する。 -C #codehighlight(C){{ #include <stdio.h> int main() { int a = 10; int b = -1; /* - なしで幅指定 */ printf("a: %5d b: %5d (default)\n", a, b); /* - ありで幅指定 */ printf("a: %-5d b: %-5d (with -)\n", a, b); /* 出力結果 a: 10 b: -1 (default) a: 10 b: -1 (with -) */ return 0; } }} -C++ #codehighlight(C){{ #include <iostream> // setwを使用する場合 #include <iomanip> int main() { int a = 10; int b = -1; // 幅のみ指定 std::cout << "a: " << std::setw(5) << a << " b: " << std::setw(5) << b << " (only setw)" << std::endl; // left で左寄せ std::cout << "a: " << std::left << std::setw(5) << a << " b: " << std::setw(5) << b << " (left)" << std::endl; /* 出力結果 a: 10 b: -1 (only setw) a: 10 b: -1 (left) */ return 0; } }} ***+ (常に符号を出力) setf(ios::showpos) または setiosflags(ios::showpos) を使用する。 -C #codehighlight(C){{ #include <stdio.h> int main() { int a = 10; int b = -1; /* + なし */ printf("a: %d b: %d (default)\n", a, b); /* + あり */ printf("a: %+d b: %+d (with +)\n", a, b); /* 出力結果 a: 10 b: -1 (without +) a: +10 b: -1 (with +) */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream> // setiosflags を使用する場合 #include <iomanip> int main() { int a = 10; int b = -1; // 指定なし std::cout << "a: " << a << " b: " << b << " (default)" << std::endl; // setiosflags で指定 std::cout << std::setiosflags(std::ios::showpos) << "a: " << a << " b: " << b << " (showpos)" << std::endl; /* 出力結果 a: 10 b: -1 (default) a: +10 b: -1 (showpos) */ return 0; } }} ***空白 (0以上の場合、符号の代わりに空白を出力) iostream 側では指定できないようなので、出力する値が0以上なら空白を出力するようにする。 -C #codehighlight(C){{ #include <stdio.h> int main() { int a = 10; int b = -1; /* 指定なし */ printf("a: %d b: %d (default)\n", a, b); /* 空白あり */ printf("a: % d b: % d (with space)\n", a, b); /* 出力結果 a: 10 b: -1 (default) a: 10 b: -1 (with space) */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream> #include <iomanip> int main() { int a = 10; int b = -1; /* 指定なし */ std::cout << "a: " << a << " b: " << b << " (default)" << std::endl; /* 正の場合は空白を出力する */ std::cout << "a: " << (a>=0?" ":"") << a << " b: " << b << " (default)" << std::endl; /* 出力結果 a: 10 b: -1 (default) a: 10 b: -1 (default) */ return 0; } }} ***0 (指定されたフィールド幅になるように0で埋める) internal、setf(ios::internal)、setiosflags(ios::internal) と setfill('0') を組み合わせる。 -C #codehighlight(C){{ #include <stdio.h> int main() { int a = 10; int b = -1; /* 指定なし */ printf("a: %d b: %d (default)\n", a, b); /* 0 あり */ printf("a: %05d b: %05d (with 0)\n", a, b); /* 出力結果 a: 10 b: -1 (default) a: 00010 b: -0001 (with 0) */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream> #include <iomanip> int main() { int a = 10; int b = -1; // 指定なし std::cout << "a: " << a << " b: " << b << " (default)" << std::endl; // internal と setfill で指定 std::cout << std::internal << std::setfill('0') << "a: " << std::setw(5) << a << " b: " << std::setw(5) << b << " (internal)" << std::endl; /* 出力結果 a: 10 b: -1 (default) a: 00010 b: -0001 (internal) */ return 0; } }} ***# (数値データを特殊な形式で出力) setf(ios::showbase) または setiosflags(iso::showbase) を指定する。 -C #codehighlight(C){{ #include <stdio.h> int main() { int a = 1234; printf("8進数: %o -> %#o\n" "16進数: %x -> %#x\n", a, a, a, a); /* 出力結果 8進数: 2322 -> 02322 16進数: 4d2 -> 0x4d2 */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream> #include <iomanip> int main() { int a = 1234; // 8進数 std::cout << std::oct << "8進数: " << a << " -> " << std::setiosflags(std::ios::showbase) << a << std::endl; // showbase のリセット std::cout.unsetf(std::ios::showbase); // 16進数 std::cout << std::hex << "16進数: " << a << " -> " << std::setiosflags(std::ios::showbase) << a << std::endl; /* 出力結果 8進数: 2322 -> 02322 16進数: 4d2 -> 0x4d2 */ return 0; } }} **最小フィールド幅 ***数値 width または setw を指定する。 -C #codehighlight(C){{ #include <stdio.h> int main() { int a = 10; int b = -1; /* 指定なし */ printf("a: %d b: %d (default)\n", a, b); /* 幅指定 */ printf("a: %5d b: %5d (with number)\n", a, b); /* 出力結果 a: 10 b: -1 (default) a: 10 b: -1 (with number) */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream> #include <iomanip> int main() { int a = 10; int b = -1; // 指定なし std::cout << "a: " << a << " b: " << b << " (default)" << std::endl; // 幅指定 std::cout << "a: " << std::setw(5) << a << " b: " << std::setw(5) << b << " (setw)" << std::endl; /* 出力結果 a: 10 b: -1 (default) a: 10 b: -1 (setw) */ return 0; } }} *** * (最小フィールド幅を引数で指定) width または setw を指定する。 -C #codehighlight(C){{ #include <stdio.h> int main() { int a = 10; int b = -1; int c = 5; /* 指定なし */ printf("a: %d b: %d (default)\n", a, b); /* 幅指定 */ printf("a: %*d b: %*d (with *)\n", c, a, c, b); /* 出力結果 a: 10 b: -1 (default) a: 10 b: -1 (with *) */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream> #include <iomanip> int main() { int a = 10; int b = -1; int c = 5; // 指定なし std::cout << "a: " << a << " b: " << b << " (default)" << std::endl; // 幅指定 std::cout << "a: " << std::setw(c) << a << " b: " << std::setw(c) << b << " (setw)" << std::endl; /* 出力結果 a: 10 b: -1 (default) a: 10 b: -1 (setw) */ return 0; } }} **精度 ***.数値 (小数点以下の桁数または最大表示文字数の指定) --整数の最小フィールド幅を指定する(0埋め付き) #div(){ 0以上の場合は0埋め付きの最小フィールド幅の指定と同じ。 負の数の場合、符号をフィールド幅にいれないことに注意。 }} --小数点以下の精度を指定する #div(){ fixed と precision または setprecision を指定する。 } --有効数字を指定する #div(){ precision または setprecision を指定する。 } --最大文字数を指定する #div(){ iostream 側では調整できないので substr で指定した文字数だけ切り出す。 } -C #codehighlight(C){{ #include <stdio.h> int main() { int a = -10; double b = 1.23; double c = 4.56; char *d = "abcdef"; /* 整数 */ printf("%d -> %.1d\n", a, a); /* 少数 */ printf("%f -> %.1f\n", b, b); printf("%g -> %.1g\n", c, c); /* 文字列 */ printf("%s -> %.1s\n", d, d); /* 出力結果 -10 -> -00010 1.230000 -> 1.2 4.56 -> 5 abcdef -> a */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream> #include <iomanip> #include <string> int main() { int a = -10; double b = 1.23; double c = 4.56; std::string d("abcdef"); /* 整数 */ std::cout << a << " -> "; std::cout << std::internal << std::setfill('0') << std::setw(5+(a>=0?0:1)) << a << std::endl; // デフォルトに戻す std::cout.unsetf(std::ios::internal); /* 少数 */ std::cout << b << " -> ";; std::cout << std::fixed << std::setprecision(1) << b << std::endl; // デフォルトに(ry std::cout << std::setprecision(6); std::cout.unsetf(std::ios::fixed); std::cout << c << " -> "; std::cout << std::setprecision(1) << c << std::endl; std::cout << d << " -> "; std::cout << d.substr(0, 1) << std::endl; /* 出力結果 -10 -> -00010 1.23 -> 1.2 4.56 -> 5 abcdef -> a */ return 0; } }} ***.0、. (小数点以下の数を表示しない) 整数の場合、0 のときの動作はiostream 側で指定できないので 条件分岐で出力しないようにする。 少数の場合、precision または setprecision に 0 を指定する。 -C #codehighlight(C){{ #include <stdio.h> int main() { int a = 0; double b = 1.23; /* 整数 */ printf("%d -> [%.0d] [%.d]\n", a, a, a); /* 少数 */ printf("%f -> [%.0f] [%.f]\n", b, b, b); /* 出力結果 0 -> [] [] 1.230000 -> [1] [1] */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream> #include <iomanip> int main() { int a = 0; double b = 1.23; /* 整数 */ std::cout << a << " -> "; std::cout << "["; if(a!=0) std::cout << a; std::cout << "]" << std::endl; /* 少数 */ std::cout << b << " -> ";; std::cout << std::fixed << std::setprecision(0) << "[" << b << "]" << std::endl; /* 出力結果 0 -> [] [] 1.230000 -> [1] [1] */ return 0; } }} ***.* (精度を引数で指定) precision または setprecision を指定する。 -C #codehighlight(C){{ #include <stdio.h> int main() { int a = -10; double b = 1.23; double c = 4.56; char *d = "abcdef"; int e = 5; int f = 1; /* 整数 */ printf("%d -> %.*d\n", a, e, a); /* 少数 */ printf("%f -> %.*f\n", b, f, b); printf("%g -> %.*g\n", c, f, c); /* 文字列 */ printf("%s -> %.*s\n", d, f, d); /* 出力結果 -10 -> -00010 1.230000 -> 1.2 4.56 -> 5 abcdef -> a */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream> #include <iomanip> #include <string> int main() { int a = -10; double b = 1.23; double c = 4.56; std::string d("abcdef"); int e = 5; int f = 1; /* 整数 */ std::cout << a << " -> "; std::cout << std::internal << std::setfill('0') << std::setw(e+(a>=0?0:1)) << a << std::endl; // デフォルトに戻す std::cout.unsetf(std::ios::internal); /* 少数 */ std::cout << b << " -> ";; std::cout << std::fixed << std::setprecision(f) << b << std::endl; // デフォルトに(ry std::cout << std::setprecision(6); std::cout.unsetf(std::ios::fixed); std::cout << c << " -> "; std::cout << std::setprecision(f) << c << std::endl; std::cout << d << " -> "; std::cout << d.substr(0, f) << std::endl; /* 出力結果 -10 -> -00010 1.23 -> 1.2 4.56 -> 5 abcdef -> a */ return 0; } }} **長さ修飾子 ***h (short int または unsigned short int として出力) ***l (long int または unsigned long int として出力) ***L (long double として出力) それぞれの型へキャストする。 **変換指定子 ***d, i (10進符号付き整数) デフォルト。 dec、setf(ios::dec)、 setiosflags(ios::dec) のいずれかを使用する。~ char型、unsigned char型を数値として出力する場合、 short型やint型にキャストする必要がある。 -C #codehighlight(C){{ #include <stdio.h> int main() { int a = 10; int b = -1; printf("%d %d\n", a, b); /* 出力結果 10 -1 */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream.h> int main() { int a = 10; int b = -1; // hex、oct を指定していない場合、dec は不要 std::cout << std::dec << a << " " << b << std::endl; /* 出力結果 10 -1 */ return 0; } }} ***u (10進符号無し整数) unsignedにキャストする。 -C #codehighlight(C){{ #include <stdio.h> int main() { int a = 10; int b = -1; printf("%u %u\n", a, b); /* 出力結果 10 4294967295 */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream.h> int main() { int a = 10; int b = -1; std::cout << static_cast<unsigned int>(a) << " " << static_cast<unsigned int>(b) << std::endl; /* 出力結果 10 4294967295 */ return 0; } }} ***o (8進符号無し整数) oct、setf(ios::oct)、 setiosflags(ios::oct) のいずれかを使用する。 -C #codehighlight(C){{ #include <stdio.h> int main() { int a = 10; int b = -1; printf("%o %o\n", a, b); /* 出力結果 12 37777777777 */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream.h> int main() { int a = 10; int b = -1; std::cout << std::oct << a << " " << b << std::endl; /* 出力結果 12 37777777777 */ return 0; } }} ***x, X (16進符号無し整数(X は大文字で出力)) hex、setf(ios::hex)、 setiosflags(ios::hex) のいずれかを使用する。~ 大文字で出力する場合、さらにuppercase、setf(ios::uppercase)、setiosflags(ios::uppercase)のいずれかを使用する。 -C #codehighlight(C){{ #include <stdio.h> int main() { int a = 10; int b = -1; printf("%x %x %X %X\n", a, b, a, b); /* 出力結果 a ffffffff A FFFFFFFF */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream.h> int main() { int a = 10; int b = -1; std::cout << std::hex << a << " " << b << " " << std::uppercase << a << " " << b << std::endl; /* 出力結果 a ffffffff A FFFFFFFF */ return 0; } }} ***e, E (指数形式浮動小数点数(E は大文字で出力)) scientific、setf(ios::scientific)、 setiosflags(ios::scientific) のいずれかを使用する。~ 大文字で出力する場合、さらにuppercase、setf(ios::uppercase)、setiosflags(ios::uppercase)のいずれかを使用する。 -C #codehighlight(C){{ #include <stdio.h> int main() { double a = 12.34; printf("%e %E\n", a, a); /* 出力結果 1.234000e+001 1.234000E+001 */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream.h> int main() { double a = 12.34; std::cout << std::scientific << a << " " << std::uppercase << a << std::endl; /* 出力結果 1.234000e+001 1.234000E+001 */ return 0; } }} ***f (小数形式浮動小数点数) fixed、setf(ios::fixed)、 setiosflags(ios::fixed) のいずれかを使用する。 6桁(デフォルト)の精度が必要ない場合はデフォルトでも可。 -C #codehighlight(C){{ #include <stdio.h> int main() { double a = 12.34; printf("%f\n", a, a); /* 出力結果 12.340000 */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream.h> int main() { double a = 12.34; std::cout << std::fixed << a << std::endl; /* 出力結果 12.340000 */ return 0; } }} ***g, G (e または f 形式のうち適した方(G は大文字で出力)) 出力する浮動小数点が0.00001 (1.0*10^-4)以下ならscientificを、そうでなければfixedを指定する。~ が、出力される精度が異なるため、厳密に同じ出力を得られない。 -C #codehighlight(C){{ #include <stdio.h> int main() { double a = 0.0001234; double b = 0.00001234; /* 指数部が-5以下なら指数表現になる */ printf("%g %g\n", a, b); /* 出力結果 0.0001234 1.234e-005 */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream.h> #include <limits> int main() { double a = 0.0001234; double b = 0.00001234; std::cout << (a*10000-1.0>std::numeric_limits<double>::epsilon() ? std::fixed : std::scientific) << a << " " << (b*10000-1.0>std::numeric_limits<double>::epsilon() ? std::fixed : std::scientific) << b << std::endl; /* 出力結果 0.000123 1.234000e-005 */ return 0; } }} ***c (文字) char型、unsigned char型にキャストする。 -C #codehighlight(C){{ #include <stdio.h> int main() { char a = 'a'; int b = 98; printf("%c %c\n", a, b); /* 出力結果 a b */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream.h> int main() { char a = 'a'; int b = 98; std::cout << a << " " << static_cast<char>(b) << std::endl; /* 出力結果 a b */ return 0; } }} ***s (文字列) char型、unsigned char型のポインタまたはstring型を出力する。 -C #codehighlight(C){{ #include <stdio.h> int main() { char *a = "abc"; printf("%s\n", a); /* 出力結果 abc */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream.h> int main() { char *a = "abc"; std::string b("abc"); std::cout << a << " " << b << std::endl; /* 出力結果 abc abc */ return 0; } }} ***p (ポインタの値) ポインタをそのまま出力する。~ ただし、char型、unsigned char型のポインタは他の型のポインタへキャストする必要がある。 -C #codehighlight(C){{ #include <stdio.h> int main() { char *a = "abc"; int i = 0; int *b = &i; printf("%p %p\n", a, b); /* 出力結果 00424024 0012FEC8 */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream.h> int main() { char *a = "abc"; int i = 0; int *b = &i; std::cout << reinterpret_cast<void *>(a) << " " << b << std::endl; /* 出力結果 0044E0CC 0012FEC8 */ return 0; } }} ***n (整数変数に出力済み文字数を格納) 一旦文字列に格納して文字列長を取得し、その後で文字列を取得する。 -C #codehighlight(C){{ #include <stdio.h> int main() { int a = 10; int b = 2; int c = 3; int n = 0; printf("%d %d <%n>%d\n", a, b, &n, c); printf("n=%d\n", n); /* 出力結果 10 2 <>3 n=6 */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream.h> int main() { int a = 10; int b = 2; int c = 3; int n = 0; std::stringstream tmp; tmp << a << " " << b << " <"; n = tmp.str().length(); std::cout << tmp.str() << ">" << c << std::endl; std::cout << "n=" << n << std::endl; /* 出力結果 10 2 <>3 n=6 */ return 0; } }} ***% ('%'の出力) そのまま出力する。 -C #codehighlight(C){{ #include <stdio.h> int main() { printf("%%\n"); /* 出力結果 % */ return 0; } }} -C++ #codehighlight(C++){{ #include <iostream.h> int main() { std::cout << '%' << std::endl; /* 出力結果 % */ return 0; } }} ----

表示オプション

横に並べて表示:
変化行の前後のみ表示:
人気記事ランキング
目安箱バナー