printf, _printf_l, wprintf, _wprintf_l
Visual Studio
Prints the formatted output to the standard output stream. for more secure versions of these functions, see printf_s, _printf_s_l, wprintf_s, _wprintf_s_l.
Grammar
int printf ( const char *format [, argument] ...); int _printf_l ( const char *format, locale_t locale [, argument] ...); int wprintf ( const wchar_t *format [, argument] ...); int _wprintf_l ( const wchar_t *format, locale_t locale [, argument] ...);
Parameters
-
Format
-
The format control.
-
Argument
-
Optional parameters.
-
Locale
-
The locale to use.
return value
Returns the number of characters printed, or returns a negative value if an error occurs. parameter Validation. " if format is NULL , invalid parameter handlers are called, as described in parameter validation . errno set to EINVAL . argument , EOF (0xFFFF), the function returns-1.
For information about errno and error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.
Notes
printffunction to format a series of characters and values and print it to a standard output streamstdoutIn format string, format string must contain a specification that determines the output format of the parameter. fprintf behave identically except that printf writes output to stdout rather than to a destination of type FILE . " > printf and fprintf have the same behavior, but printf writes output to &NBSP stdout instead of FILE type target.
wprintf is a wide-character version of printf ; format is a wide string. If the stream is open in ANSI mode, wprintf and printf will have the same behavior. printf currently does not support output to a UNICODE stream.
The versions of these functions with the _l suffix are the same, except that they use the locale parameter passed instead of the current thread locale.
Generic Text routine mapping
tchar. H routines |
undefined _unicode & _mbcs |
defined _mbcs |
defined _unicode |
_tprintf |
< p> printf |
printf |
wprintf |
The format parameter includes ordinary characters, escape sequences, and (if the parameters are formatted) a formal specification. A sequence that follows its appearance copies ordinary characters and escape sequences to stdout. For example, line:
Build output:
Line one line
The format specification always takes a percent sign (%) starts and reads from left to right.WhenprintfWhen the first format specification (if any) is encountered, it transformsformatAfter the value of the first parameter and outputs it accordingly. The second format specification causes the second parameter to be converted and output, and so on. If you have more arguments than the format specification, the extra arguments are ignored. If all the format specifications do not have sufficient parameters, the result is undefined.
Security Note |
Make sure that format is not a user-defined string. |
Generic Text routine mapping
Tchar.h Routines |
_unicode and _MBCS not defined |
_mbcs defined |
_unicode defined |
_tprintf |
Printf |
Printf |
wprintf |
_tprintf_l |
_printf_l |
_printf_l |
_wprintf_l |
Requirements
Routines |
Required headers |
printf, _printf_l |
<stdio.h> |
wprintf, _wprintf_l |
<stdio.h> or <wchar.h> |
The console is not supported in Windows 8.x store apps. standard flow handles associated with the console stdin,stdout , and stderr must be redirected, and then C run-time functions can be used in Windows 8.x store applications. for more information about compatibility, see compatibility.
Example
crt_printf.c//the uses the printf and wprintf functions//to produce formatted output. #include <stdio.h> ; int main (void) {char ch = ' h ', *string = "Computer"; wchar_t WCH = L ' W ', *wstring = L "Unicode"; int count =-9234; Double fp = 251.7366; Display integers printf ("Integer formats:\n" "Decimal:%d Justified:%.6d" "Unsigned:%u\n ", Count, Count, Count, count); Display decimals printf ("Decimal%d as:\n Hex:%xh" "C hex:0x%x octal:%o\n", Count, Coun T, Count, Count); Display in different radixes printf ("Digits equal:\n Hex:%i" "Octal:%i Decimal:%i\n", 0x10, 010, 10); Display characters printf ("Characters in field (1): \ n" "%10c%5hc%5c%5lc\n", CH, ch, WCH, WCH); wprintf (L "Characters in Field (2): \ n" L "%10c%5hc%5c%5lc\n", CH, ch, WCH, WCH); Display strings printf ("Strings in field (1): \n%25s\n" "%25.4hs\n%s%25.3ls\n", String, String, wstring, wstring); wprintf (L "Strings in Field (2): \n%25s\n" L "%25.4hs\n%s%25.3ls\n", String, String, wstring, Wstrin g); Display Real numbers printf ("Real numbers:\n%f%.2f%e%e\n", FP, FP, FP, FP); Display pointer printf ("\naddress as:%p\n", &count);}
Sample Output
Integer formats: Decimal: -9234 justified: -009234 unsigned:4294958062decimal-9234 as: Hex: Ffffdbeeh C Hex:0xffffdbee octal:37777755756digits equal: hex:16 octal:8 Decimal: 10Characters in field (1): h H w wcharacters in field (2): h H w wstrings in Field (1): computer Comp Unicode unistrings in field (2): computer Comp Unicode unireal numbers: 251.736600 251.74 2.517366e+002 2.517366e+002address as: 0012ff3c
. NET Framework Equivalents
Please see
Floating point Support
Stream I/O
Regional Settings
fopen, _wfopen
_fprintf_p, _fprintf_p_l, _fwprintf_p, _fwprintf_p_l
scanf, _scanf_l, wscanf, _wscanf_l
sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l
vprintf function
_set_output_format
_tprintf from MSDN