printf, wprintf
Remonter à Stdio.h - Index
Header File
stdio.h
Category
Console I/O Routines
Prototype
int printf(const char *format[, argument, ...]);
int wprintf(const wchar_t *format[, argument, ...]);
Description
Writes formatted output to stdout.
The printf function:
- Accepts a series of arguments
- Applies to each argument a format specifier contained in the format string *format
- Outputs the formatted data (to the screen, a stream, stdout, or a string)
There must be enough arguments for the format. If there are not, the results will be unpredictable and likely disastrous. Excess arguments (more than required by the format) are merely ignored.
Note: For Win32 and Win64 GUI applications, stdout must be redirected.
Return Value
On success, printf returns the number of bytes output.
On error, printf returns EOF.
More About printf
Example
#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;
}
Output
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
Portability
POSIX | Win32 | ANSI C | ANSI C++ | Win64 | |
---|---|---|---|---|---|
printf |
+ |
+ |
+ |
+ |
+ |
_wprintf |
+ |
+ |