printf、wprintf
Stdlib.h:インデックス への移動
ヘッダー ファイル
stdio.h
カテゴリ
コンソール入出力ルーチン
プロトタイプ
int printf(const char *format[, argument, ...]);
int wprintf(const wchar_t *format[, argument, ...]);
説明
書式付き出力を stdout に書き込みます。
printf 関数は:
書式に合わせて十分な数の引数を指定する必要があります。引数が不足している場合、結果は予測できず、重大な結果を招く可能性があります。余分な引数(書式で必要なもの以外)はただ無視されるだけです。
メモ: Win32 および Win64 GUI アプリケーションの場合は、stdout をリダイレクトする必要があります。
戻り値
正常に終了した場合、printf は、出力されたバイト数を返します。
エラーが発生した場合、printf は EOF を返します。
printf の詳細
例
#include <stdio.h>
#include <tchar.h>
int _tmain(int argc, _TCHAR* argv[]) {
int number = -8542;
int countchars;
float realnumber = 375.22352;
char letter = 'a';
wchar_t wletter = L'b';
char* arrayofchars = "This is a sample text";
wchar_t* warrayofchars = L"This is a wide sample text";
/* Printing integers */
printf(" Integer: %d\n Unsigned: %u\n Hexadecimal: %x\n Octal: %o\n\n ",
number, number, number, number);
/* Printing floating point numbers */
printf("Float: %f\n With precision specifier: %.2f\n Scientific: %e\n\n",
realnumber, realnumber, realnumber, realnumber);
/* Printing characters with printf and wprintf */
printf(" printf:\n %c %hc %C %lc\n", letter, letter, wletter, wletter);
wprintf(L" wprintf:\n %C %c %c %lc\n\n", letter, letter, wletter, wletter);
/* Printing strings */
printf(" String (printf):\n %s \n %35s \n %10.6s \n\n ", arrayofchars, arrayofchars, arrayofchars);
wprintf(L"Wide string (wprintf):\n %s \n %44s \n %11.12s \n \n", warrayofchars, warrayofchars, warrayofchars);
/* Printing pointers */
printf(" Address of %d as pointer: %p\n", number, &number);
/* Counts the printed characters until the %n format specifier is encountered*/
printf( "\n Counting the printed characters until the format specifier is encountered:\n" );
printf( "15382%n123456789\n", &countchars );
printf( "Number of characters: %d\n\n", countchars );
getchar();
return 0;
}
出力
Integer: -8542
Unsigned: 4294958754
Hexadecimal: ffffdea2
Octal: 37777757242
Float: 375.223511
With precision specifier: 375.22
Scientific: 3.752235e+03
printf:
a a b b
wprintf:
a a b b
String (printf):
This is a sample text
This is a sample text
This i
Wide string (wprintf):
This is a wide sample text
This is a wide sample text
This is a wi
Address of -8542 as pointer: 0018FF50
Counting the printed characters until the format specifier is encountered:
15382123456789
Number of characters: 5
移植性
POSIX | Win32 | ANSI C | ANSI C++ | Win64 | |
---|---|---|---|---|---|
printf |
+ |
+ |
+ |
+ |
+ |
_wprintf |
+ |
+ |