?
This document uses PHP Chinese website manual Release
在頭文件<wchar.h>中定義 | ||
---|---|---|
(1) | ||
int wprintf(const wchar_t * format,...); | (自C95以來(lái))(直到C99) | |
int wprintf(const wchar_t * restrict format,...); | (自C99以來(lái)) | |
(2) | ||
int fwprintf(FILE * stream,const wchar_t * format,...); | (自C95以來(lái))(直到C99) | |
int fwprintf(FILE * restrict stream,const wchar_t * restrict format,...); | (自C99以來(lái)) | |
(3) | ||
int swprintf(wchar_t * buffer,size_t bufsz,const wchar_t * format,...); | (自C95以來(lái))(直到C99) | |
int swprintf(wchar_t * restrict buffer,size_t bufsz,const wchar_t * restrict format,...); | (自C99以來(lái)) | |
int wprintf_s(const wchar_t * restrict format,...); | (4) | (自C11以來(lái)) |
int fwprintf_s(FILE * restrict stream,const wchar_t * restrict format,...); | (5) | (自C11以來(lái)) |
int swprintf_s(wchar_t * restrict buffer,rsize_t bufsz,const wchar_t * restrict format,...); | (6) | (自C11以來(lái)) |
int snwprintf_s(wchar_t * restrict s,rsize_t n,const wchar_t * restrict format,...); | (7) | (自C11以來(lái)) |
從給定的位置加載數(shù)據(jù),將它們轉(zhuǎn)換為寬字符串等值并將結(jié)果寫(xiě)入各種接收器。
1)將結(jié)果寫(xiě)入stdout
。
2)將結(jié)果寫(xiě)入文件流stream
。
3)如果bufsz
大于零,則將結(jié)果寫(xiě)入寬字符串buffer
。最bufsz-1
寬字符后面跟著空寬字符。如果bufsz
為零,則不會(huì)寫(xiě)入任何內(nèi)容(并且buffer
可能是空指針),但返回值(將寫(xiě)入的寬字符數(shù))仍然會(huì)計(jì)算并返回。
4-6)與(1-3)相同,但在運(yùn)行時(shí)檢測(cè)到以下錯(cuò)誤并調(diào)用當(dāng)前安裝的約束處理函數(shù):
轉(zhuǎn)換說(shuō)明符%n
存在于format
任何對(duì)應(yīng)的參數(shù)%s
都是空指針
format
或者buffer
是空指針
bufsz
是零或大于 RSIZE_MAX/sizeof(wchar_t)
編碼錯(cuò)誤出現(xiàn)在任何字符串和字符轉(zhuǎn)換說(shuō)明符中
(僅限于swprintf_s
)要寫(xiě)入的寬字符數(shù)(包括null)將超過(guò)bufsz
。
7)與(6)相同,只是它會(huì)截?cái)嘟Y(jié)果以適應(yīng)s所指向的數(shù)組。由于所有的邊界檢查功能,wprintf_s
,wfprintf_s
,和wsprintf_s
僅保證可供如果__STDC_LIB_EXT1__
由實(shí)現(xiàn)所定義,并且如果用戶定義__STDC_WANT_LIB_EXT1__
的整數(shù)常數(shù)1
,包括之前<stdio.h>
。
流 | - | 輸出文件流寫(xiě)入 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
緩沖 | - | 指向要寫(xiě)入的寬字符字符串的指針 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bufsz | - | 最多可以寫(xiě)入bufsz-1寬字符,加上空終止符 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
格式 | - | 指向以空字符結(jié)尾的寬字符串的指針,指定如何解釋數(shù)據(jù)。格式字符串由普通的寬字符(%除外)組成,它們被不變地復(fù)制到輸出流中,以及轉(zhuǎn)換規(guī)范。每個(gè)轉(zhuǎn)換規(guī)范具有以下格式:介紹%字符(可選)一個(gè)或多個(gè)標(biāo)志,用于修改轉(zhuǎn)換的行為: - :轉(zhuǎn)換結(jié)果在字段內(nèi)左對(duì)齊(默認(rèn)情況下,它是右對(duì)齊的)+ :帶符號(hào)轉(zhuǎn)換的符號(hào)總是作為轉(zhuǎn)換結(jié)果的前綴(默認(rèn)情況下,只有當(dāng)結(jié)果為負(fù)時(shí),結(jié)果才以負(fù)值開(kāi)頭)space:如果帶符號(hào)轉(zhuǎn)換的結(jié)果不是以符號(hào)字符開(kāi)頭,或者是空的,空間是預(yù)先考慮的結(jié)果。如果存在+標(biāo)志,則忽略它。#:執(zhí)行轉(zhuǎn)換的替代形式。請(qǐng)參閱下表以了解確切的效果,否則行為未定義。0:對(duì)于整數(shù)和浮點(diǎn)數(shù)轉(zhuǎn)換,前導(dǎo)零用于填充字段而不是空格字符。對(duì)于整數(shù),如果明確指定了精度,它將被忽略。對(duì)于使用此標(biāo)志的其他轉(zhuǎn)換會(huì)導(dǎo)致未定義的行為。如果 - 標(biāo)志存在,它將被忽略。(可選)整數(shù)值或*指定最小字段寬度。如果需要,結(jié)果會(huì)填充空格字符(默認(rèn)情況下),右側(cè)對(duì)齊時(shí)填充空白字符,左側(cè)填充右側(cè)填充。在使用*的情況下,寬度由類型為int的附加參數(shù)指定。如果參數(shù)的值是負(fù)值,那么結(jié)果是指定 - 標(biāo)志和正字段寬度。(注意:這是最小寬度:該值從不被截?cái)唷#蛇x)。后跟整數(shù)或*,或者兩者都不指定轉(zhuǎn)換的精度。在使用*的情況下,精度由類型為int的附加參數(shù)指定。如果這個(gè)參數(shù)的值是負(fù)數(shù),它將被忽略。如果既不使用數(shù)字也不使用*,則精度取為零。請(qǐng)參閱下表以了解精確度的確切影響。(可選)長(zhǎng)度修飾符,用于指定參數(shù)轉(zhuǎn)換格式說(shuō)明符的大小以下格式說(shuō)明符可用:轉(zhuǎn)換說(shuō)明符說(shuō)明參數(shù)類型長(zhǎng)度修飾符hh(C99)。h(無(wú))ll(C99)。j(C99)。z(C99)。t(C99)。L%寫(xiě)文字%。完整的轉(zhuǎn)換規(guī)范必須是%%。不適用不適用不適用不適用不適用不適用不適用不適用不適用單個(gè)字符。該參數(shù)首先轉(zhuǎn)換為wchar_t,就好像通過(guò)調(diào)用btowc一樣。如果使用l修飾符,則首先將wint_t參數(shù)轉(zhuǎn)換為wchar_t。N / AN / A int wint_t N / AN / AN / AN / AN / A s寫(xiě)入字符串參數(shù)必須是一個(gè)指向包含多字節(jié)字符序列的字符數(shù)組的初始元素的指針,該字符序列從初始轉(zhuǎn)換狀態(tài)開(kāi)始,被轉(zhuǎn)換為寬字符數(shù)組,就像通過(guò)調(diào)用mbrtowc以零初始化轉(zhuǎn)換狀態(tài)一樣。Precision指定要寫(xiě)入的最大寬字符數(shù)。如果未指定Precision,則將每個(gè)寬字符寫(xiě)入并不包括第一個(gè)空終止符。如果使用l說(shuō)明符,參數(shù)必須是指向wchar_t數(shù)組的初始元素的指針。N / AN / A char * wchar_t * N / AN / AN / AN / AN / A di將有符號(hào)整數(shù)轉(zhuǎn)換為十進(jìn)制表示形式-dddd。精度指定出現(xiàn)的最小位數(shù)。默認(rèn)精度為1.如果轉(zhuǎn)換值和精度均為0,則轉(zhuǎn)換不會(huì)生成任何字符。signed char short int long long long intmax_t signed size_t ptrdiff_t不適用o將無(wú)符號(hào)整數(shù)轉(zhuǎn)換為八進(jìn)制表示oooo。精度指定出現(xiàn)的最小位數(shù)。默認(rèn)精度為1.如果轉(zhuǎn)換值和精度均為0,則轉(zhuǎn)換不會(huì)生成任何字符。在替代實(shí)現(xiàn)中,如果需要,可以增加精度以寫(xiě)入一個(gè)前導(dǎo)零。在這種情況下,如果轉(zhuǎn)換值和精度均為0,則寫(xiě)入單個(gè)0。unsigned char unsigned short unsigned int unsigned long unsigned long long uintmax_t size_t ptrdiff_t的無(wú)符號(hào)版本不適用x X將無(wú)符號(hào)整數(shù)轉(zhuǎn)換為十六進(jìn)制表示形式hhhh。對(duì)于x轉(zhuǎn)換字母abcdef被使用。對(duì)于X轉(zhuǎn)換字母使用ABCDEF。精度指定出現(xiàn)的最小位數(shù)。默認(rèn)精度為1.如果轉(zhuǎn)換值和精度均為0,則轉(zhuǎn)換不會(huì)生成任何字符。在替代實(shí)現(xiàn)中,如果轉(zhuǎn)換后的值為非零值,則0x或0X將作為結(jié)果的前綴。不適用u將無(wú)符號(hào)整數(shù)轉(zhuǎn)換為十進(jìn)制表示形式dddd。精度指定出現(xiàn)的最小位數(shù)。默認(rèn)精度為1。如果轉(zhuǎn)換值和精度均為0,則轉(zhuǎn)換不會(huì)生成字符。N / A f F將浮點(diǎn)數(shù)轉(zhuǎn)換為樣式-ddd.ddd中的十進(jìn)制表示法。精度指定小數(shù)點(diǎn)后面出現(xiàn)的最小位數(shù)。默認(rèn)精度為6.在替代實(shí)現(xiàn)中,即使沒(méi)有數(shù)字跟隨,小數(shù)點(diǎn)字符也會(huì)被寫(xiě)入。對(duì)于無(wú)限和非數(shù)字轉(zhuǎn)換風(fēng)格,請(qǐng)參閱注釋。N / AN / A double double(C99)N / AN / AN / AN / A long double e E將浮點(diǎn)數(shù)轉(zhuǎn)換為十進(jìn)制指數(shù)表示法。對(duì)于e轉(zhuǎn)換樣式,使用-d.ddde±dd。對(duì)于E轉(zhuǎn)換樣式,使用-d.dddE±dd。指數(shù)至少包含兩位數(shù)字,只有在必要時(shí)才使用更多數(shù)字。如果該值為0,則指數(shù)也為0。精度指定小數(shù)點(diǎn)后面出現(xiàn)的最小位數(shù)。默認(rèn)精度為6.在替代實(shí)現(xiàn)中,即使沒(méi)有數(shù)字跟隨,小數(shù)點(diǎn)字符也會(huì)被寫(xiě)入。對(duì)于無(wú)限和非數(shù)字轉(zhuǎn)換風(fēng)格,請(qǐng)參閱注釋。不適用不適用不適用不適用不適用不適用(C99)。將浮點(diǎn)數(shù)轉(zhuǎn)換為十六進(jìn)制指數(shù)表示法。對(duì)于轉(zhuǎn)換類型,使用-0xh.hhhp±d。對(duì)于A轉(zhuǎn)換樣式,使用-0Xh.hhhP±d。如果參數(shù)不是標(biāo)準(zhǔn)化的浮點(diǎn)值,則第一個(gè)十六進(jìn)制數(shù)字為0。如果該值為0,則指數(shù)也為0。精度指定小數(shù)點(diǎn)后面出現(xiàn)的最小位數(shù)。默認(rèn)精度足以精確表示值。在替代實(shí)現(xiàn)中,即使沒(méi)有數(shù)字跟隨,小數(shù)點(diǎn)字符也會(huì)被寫(xiě)入。對(duì)于無(wú)限和非數(shù)字轉(zhuǎn)換風(fēng)格,請(qǐng)參閱注釋。不可用/不可用/不可用/不可用/ g G根據(jù)值和精度將浮點(diǎn)數(shù)轉(zhuǎn)換為十進(jìn)制或十進(jìn)制指數(shù)符號(hào)。對(duì)于使用樣式e或f的g轉(zhuǎn)換樣式轉(zhuǎn)換將被執(zhí)行。對(duì)于G轉(zhuǎn)換樣式,將使用樣式E或F進(jìn)行轉(zhuǎn)換。假設(shè)P等于非零的精度,如果未指定精度,則等于6;如果精度為0,則等于1。然后,如果具有樣式E的轉(zhuǎn)換將具有X的指數(shù):如果P> X≥-4,則轉(zhuǎn)換具有樣式f或F和精度P-1-X。否則,轉(zhuǎn)換具有樣式e或E以及精度P - 1。除非請(qǐng)求替代表示,否則尾隨零將被刪除,如果沒(méi)有剩余小數(shù)部分,小數(shù)點(diǎn)字符也會(huì)被刪除。對(duì)于無(wú)限和非數(shù)字轉(zhuǎn)換風(fēng)格,請(qǐng)參閱注釋。不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用 結(jié)果寫(xiě)入?yún)?shù)指向的值。規(guī)范可能不包含任何標(biāo)志,字段寬度或精度。signed char * short * int * long * long long * intmax_t * signed size_t * ptrdiff_t * N / A p寫(xiě)一個(gè)實(shí)現(xiàn)定義的字符序列來(lái)定義一個(gè)指針。不適用不適用不適用不適用不適用不適用不適用浮點(diǎn)轉(zhuǎn)換函數(shù)將無(wú)窮大轉(zhuǎn)換為inf或無(wú)窮大。使用哪一個(gè)是實(shí)現(xiàn)定義的。非數(shù)字轉(zhuǎn)換為南或南(char_sequence)。使用哪一個(gè)是實(shí)現(xiàn)定義的。轉(zhuǎn)換F,E,G,A代替輸出INF,INFINITY,NAN。盡管%c需要int參數(shù),但因?yàn)樵谡{(diào)用可變參數(shù)函數(shù)時(shí)發(fā)生整數(shù)提升,所以傳遞char是安全的。固定寬度字符類型(int8_t等)的正確轉(zhuǎn)換規(guī)范在標(biāo)頭<inttypes.h>中定義(盡管PRIdMAX,PRIuMAX等與%jd,%ju等同義)。內(nèi)存寫(xiě)入轉(zhuǎn)換說(shuō)明符%n是格式字符串依賴用戶輸入并且不受邊界檢查的printf_s系列函數(shù)支持的安全漏洞的常見(jiàn)目標(biāo)。每個(gè)轉(zhuǎn)換說(shuō)明符的操作之后都有一個(gè)序列點(diǎn); 這允許將多個(gè)%n結(jié)果存儲(chǔ)在相同的變量中,或者,作為邊緣情況,在相同的調(diào)用中打印由早先的%n修改的字符串。如果轉(zhuǎn)換規(guī)范無(wú)效,則行為未定義。 | Conversion specifier | Explanation | Argument type | length modifier | hh (C99). | h | (none) | l | ll (C99). | j (C99). | z (C99). | t (C99). | L | % | writes literal %. The full conversion specification must be %%. | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | c | writes a single character. The argument is first converted to wchar_t as if by calling btowc. If the l modifier is used, the wint_t argument is first converted to wchar_t. | N/A | N/A | int | wint_t | N/A | N/A | N/A | N/A | N/A | s | writes a character string The argument must be a pointer to the initial element of a character array containing a multibyte character sequence beginning in the initial shift state, which is converted to wide character array as if by a call to mbrtowc with zero-initialized conversion state. Precision specifies the maximum number of wide characters to be written. If Precision is not specified, writes every wide characters up to and not including the first null terminator. If the l specifier is used, the argument must be a pointer to the initial element of an array of wchar_t. | N/A | N/A | char* | wchar_t* | N/A | N/A | N/A | N/A | N/A | d i | converts a signed integer into decimal representation -dddd. Precision specifies the minimum number of digits to appear. The default precision is 1. If both the converted value and the precision are 0 the conversion results in no characters. | signed char | short | int | long | long long | intmax_t | signed size_t | ptrdiff_t | N/A | o | converts a unsigned integer into octal representation oooo. Precision specifies the minimum number of digits to appear. The default precision is 1. If both the converted value and the precision are 0 the conversion results in no characters. In the alternative implementation precision is increased if necessary, to write one leading zero. In that case if both the converted value and the precision are 0, single 0 is written. | unsigned char | unsigned short | unsigned int | unsigned long | unsigned long long | uintmax_t | size_t | unsigned version of ptrdiff_t | N/A | x X | converts an unsigned integer into hexadecimal representation hhhh. For the x conversion letters abcdef are used. For the X conversion letters ABCDEF are used. Precision specifies the minimum number of digits to appear. The default precision is 1. If both the converted value and the precision are 0 the conversion results in no characters. In the alternative implementation 0x or 0X is prefixed to results if the converted value is nonzero. | N/A | u | converts an unsigned integer into decimal representation dddd. Precision specifies the minimum number of digits to appear. The default precision is 1. If both the converted value and the precision are 0 the conversion results in no characters. | N/A | f F | converts floating-point number to the decimal notation in the style -ddd.ddd. Precision specifies the minimum number of digits to appear after the decimal point character. The default precision is 6. In the alternative implementation decimal point character is written even if no digits follow it. For infinity and not-a-number conversion style see notes. | N/A | N/A | double | double (C99) | N/A | N/A | N/A | N/A | long double | e E | converts floating-point number to the decimal exponent notation. For the e conversion style -d.ddde±dd is used. For the E conversion style -d.dddE±dd is used. The exponent contains at least two digits, more digits are used only if necessary. If the value is 0, the exponent is also 0. Precision specifies the minimum number of digits to appear after the decimal point character. The default precision is 6. In the alternative implementation decimal point character is written even if no digits follow it. For infinity and not-a-number conversion style see notes. | N/A | N/A | N/A | N/A | N/A | N/A | a A (C99). | converts floating-point number to the hexadecimal exponent notation. For the a conversion style -0xh.hhhp±d is used. For the A conversion style -0Xh.hhhP±d is used. The first hexadecimal digit is 0 if the argument is not a normalized floating point value. If the value is 0, the exponent is also 0. Precision specifies the minimum number of digits to appear after the decimal point character. The default precision is sufficient for exact representation of the value. In the alternative implementation decimal point character is written even if no digits follow it. For infinity and not-a-number conversion style see notes. | N/A | N/A | N/A | N/A | N/A | N/A | g G | converts floating-point number to decimal or decimal exponent notation depending on the value and the precision. For the g conversion style conversion with style e or f will be performed. For the G conversion style conversion with style E or F will be performed. Let P equal the precision if nonzero, 6 if the precision is not specified, or 1 if the precision is 0. Then, if a conversion with style E would have an exponent of X: if P > X ≥ ?4, the conversion is with style f or F and precision P ? 1 ? X. otherwise, the conversion is with style e or E and precision P ? 1. Unless alternative representation is requested the trailing zeros are removed, also the decimal point character is removed if no fractional part is left. For infinity and not-a-number conversion style see notes. | N/A | N/A | N/A | N/A | N/A | N/A | n | returns the number of characters written so far by this call to the function. The result is written to the value pointed to by the argument. The specification may not contain any flag, field width, or precision. | signed char* | short* | int* | long* | long long* | intmax_t* | signed size_t* | ptrdiff_t* | N/A | p | writes an implementation defined character sequence defining a pointer. | N/A | N/A | void* | N/A | N/A | N/A | N/A | N/A | N/A |
Conversion specifier | Explanation | Argument type | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
length modifier | hh (C99). | h | (none) | l | ll (C99). | j (C99). | z (C99). | t (C99). | L | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
% | writes literal %. The full conversion specification must be %%. | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
c | writes a single character. The argument is first converted to wchar_t as if by calling btowc. If the l modifier is used, the wint_t argument is first converted to wchar_t. | N/A | N/A | int | wint_t | N/A | N/A | N/A | N/A | N/A | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
s | writes a character string The argument must be a pointer to the initial element of a character array containing a multibyte character sequence beginning in the initial shift state, which is converted to wide character array as if by a call to mbrtowc with zero-initialized conversion state. Precision specifies the maximum number of wide characters to be written. If Precision is not specified, writes every wide characters up to and not including the first null terminator. If the l specifier is used, the argument must be a pointer to the initial element of an array of wchar_t. | N/A | N/A | char* | wchar_t* | N/A | N/A | N/A | N/A | N/A | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
d i | converts a signed integer into decimal representation -dddd. Precision specifies the minimum number of digits to appear. The default precision is 1. If both the converted value and the precision are 0 the conversion results in no characters. | signed char | short | int | long | long long | intmax_t | signed size_t | ptrdiff_t | N/A | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
o | converts a unsigned integer into octal representation oooo. Precision specifies the minimum number of digits to appear. The default precision is 1. If both the converted value and the precision are 0 the conversion results in no characters. In the alternative implementation precision is increased if necessary, to write one leading zero. In that case if both the converted value and the precision are 0, single 0 is written. | unsigned char | unsigned short | unsigned int | unsigned long | unsigned long long | uintmax_t | size_t | unsigned version of ptrdiff_t | N/A | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
x X | converts an unsigned integer into hexadecimal representation hhhh. For the x conversion letters abcdef are used. For the X conversion letters ABCDEF are used. Precision specifies the minimum number of digits to appear. The default precision is 1. If both the converted value and the precision are 0 the conversion results in no characters. In the alternative implementation 0x or 0X is prefixed to results if the converted value is nonzero. | N/A | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
u | converts an unsigned integer into decimal representation dddd. Precision specifies the minimum number of digits to appear. The default precision is 1. If both the converted value and the precision are 0 the conversion results in no characters. | N/A | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
f F | converts floating-point number to the decimal notation in the style -ddd.ddd. Precision specifies the minimum number of digits to appear after the decimal point character. The default precision is 6. In the alternative implementation decimal point character is written even if no digits follow it. For infinity and not-a-number conversion style see notes. | N/A | N/A | double | double (C99) | N/A | N/A | N/A | N/A | long double | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
e E | converts floating-point number to the decimal exponent notation. For the e conversion style -d.ddde±dd is used. For the E conversion style -d.dddE±dd is used. The exponent contains at least two digits, more digits are used only if necessary. If the value is 0, the exponent is also 0. Precision specifies the minimum number of digits to appear after the decimal point character. The default precision is 6. In the alternative implementation decimal point character is written even if no digits follow it. For infinity and not-a-number conversion style see notes. | N/A | N/A | N/A | N/A | N/A | N/A | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
a A (C99). | 將浮點(diǎn)數(shù)轉(zhuǎn)換為十六進(jìn)制指數(shù)表示法。對(duì)于轉(zhuǎn)換類型,使用-0xh.hhhp±d。對(duì)于A轉(zhuǎn)換樣式,使用-0Xh.hhhP±d。如果參數(shù)不是標(biāo)準(zhǔn)化的浮點(diǎn)值,則第一個(gè)十六進(jìn)制數(shù)字為0。如果該值為0,則指數(shù)也為0。精度指定小數(shù)點(diǎn)后面出現(xiàn)的最小位數(shù)。默認(rèn)精度足以精確表示值。在替代實(shí)現(xiàn)中,即使沒(méi)有數(shù)字跟隨它,也會(huì)寫(xiě)入小數(shù)點(diǎn)字符。對(duì)于無(wú)窮大和非數(shù)字轉(zhuǎn)換風(fēng)格,請(qǐng)參閱注釋。 | N / A | N / A | N / A | N / A | N / A | N / A | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
g G | 根據(jù)值和精度將浮點(diǎn)數(shù)轉(zhuǎn)換為十進(jìn)制或十進(jìn)制指數(shù)符號(hào)。對(duì)于使用樣式e或f的g轉(zhuǎn)換樣式轉(zhuǎn)換將被執(zhí)行。對(duì)于G轉(zhuǎn)換樣式,將使用樣式E或F進(jìn)行轉(zhuǎn)換。假設(shè)P等于非零的精度,如果未指定精度,則等于6;如果精度為0,則等于1。然后,如果具有樣式E的轉(zhuǎn)換將具有X的指數(shù):如果P> X≥-4,則轉(zhuǎn)換具有樣式f或F和精度P-1-X。否則,轉(zhuǎn)換具有樣式e或E以及精度P - 1.除非要求替代表示,否則尾隨零將被刪除,如果沒(méi)有剩余小數(shù)部分,小數(shù)點(diǎn)字符也會(huì)被刪除。對(duì)于無(wú)窮大和非數(shù)字轉(zhuǎn)換風(fēng)格,請(qǐng)參閱注釋。 | N / A | N / A | N / A | N / A | N / A | N / A | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
? | 將此調(diào)用到目前為止寫(xiě)入的字符數(shù)返回給該函數(shù)。結(jié)果寫(xiě)入?yún)?shù)指向的值。規(guī)范可能不包含任何標(biāo)志,字段寬度或精度。 | 簽名字符* | 短* | INT * | long* | 很長(zhǎng)* | *將intmax_t | 簽名size_t * | ptrdiff_t的* | N / A | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
p | 寫(xiě)一個(gè)實(shí)現(xiàn)定義的字符序列來(lái)定義一個(gè)指針。 | N / A | N / A | 無(wú)效* | N / A | N / A | N / A | N / A | N / A | N / A | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
... | - | 指定要打印的數(shù)據(jù)的參數(shù) |
介紹%
人物
(可選)一個(gè)或多個(gè)修改轉(zhuǎn)換行為的標(biāo)志:
-
:轉(zhuǎn)換的結(jié)果在字段內(nèi)左對(duì)齊(默認(rèn)情況下它是右對(duì)齊的)
+
:帶符號(hào)轉(zhuǎn)換的符號(hào)總是預(yù)設(shè)為轉(zhuǎn)換結(jié)果的前綴(默認(rèn)情況下結(jié)果前面為減號(hào),僅當(dāng)它為負(fù)值時(shí))
空格:如果簽名轉(zhuǎn)換的結(jié)果不是以符號(hào)字符開(kāi)頭,或者是空的,則空格會(huì)預(yù)設(shè)為結(jié)果。如果+
存在標(biāo)志,則忽略它。
#
:執(zhí)行轉(zhuǎn)換的替代形式。請(qǐng)參閱下表以了解確切的效果,否則行為未定義。
0
:對(duì)于整數(shù)和浮點(diǎn)數(shù)轉(zhuǎn)換,前導(dǎo)零用于填充字段而不是空格字符。對(duì)于整數(shù),如果明確指定了精度,它將被忽略。對(duì)于使用此標(biāo)志的其他轉(zhuǎn)換會(huì)導(dǎo)致未定義的行為。如果-
存在標(biāo)志,則忽略它。
(可選)整數(shù)值或*
指定最小字段寬度。如果需要,結(jié)果會(huì)填充空格字符(默認(rèn)情況下),右側(cè)對(duì)齊時(shí)填充空白字符,左側(cè)填充右側(cè)填充。在使用的情況下*
,寬度由類型的附加參數(shù)指定int
。如果參數(shù)的值是負(fù)數(shù),則結(jié)果是-
指定的標(biāo)志和正的字段寬度。(注意:這是最小寬度:該值從不被截?cái)?。?/p>
(可選).
后面跟隨整數(shù)或者*
或者既不指定轉(zhuǎn)換的精度。在使用的情況下*
,精度由類型的附加參數(shù)指定int
。如果這個(gè)參數(shù)的值是負(fù)數(shù),它將被忽略。如果既不使用數(shù)字也不*
使用,則精度取為零。請(qǐng)參閱下表以了解精確度的確切影響。
(可選)長(zhǎng)度修飾符,用于指定參數(shù)的大小
轉(zhuǎn)換格式說(shuō)明符
以下格式說(shuō)明符可用:
Conversion
說(shuō)明符說(shuō)明參數(shù)類型長(zhǎng)度修飾符 hh
(C99)。
h
(none) l
ll
(C99).
j
(C99).
z
(C99).
t
(C99).
L
%
寫(xiě)文字%
。完整的轉(zhuǎn)換規(guī)范必須是%%
。N / AN / AN / AN / AN / AN / AN / AN / AN / A c
寫(xiě)入單個(gè)字符。參數(shù)首先被轉(zhuǎn)換wchar_t
為仿佛通過(guò)調(diào)用btowc
。如果使用l修飾符,wint_t
則首先將參數(shù)轉(zhuǎn)換為wchar_t
。
N/A N/A int
wint_t
N / AN / AN / AN / AN / A s
寫(xiě)入字符串參數(shù)必須是一個(gè)指向字符數(shù)組初始元素的指針,該字符數(shù)組包含一個(gè)多字節(jié)字符序列,從初始移位狀態(tài)開(kāi)始,轉(zhuǎn)換為寬字符數(shù)組,如同通過(guò)調(diào)用mbrtowc
具有零初始化轉(zhuǎn)換狀態(tài)。Precision指定要寫(xiě)入的最大寬字符數(shù)。如果未指定Precision,則將每個(gè)寬字符寫(xiě)入并不包括第一個(gè)空終止符。如果使用l說(shuō)明符,參數(shù)必須是指向數(shù)組的初始元素的指針wchar_t
。
N/A N/A char*
wchar_t*
N/A N/A N/A N/A N/A d
i
將有符號(hào)的整數(shù)轉(zhuǎn)換為十進(jìn)制表示形式-dddd。 精度指定出現(xiàn)的最小位數(shù)。默認(rèn)的精度是1
。
如果轉(zhuǎn)換后的值和精度都是0
沒(méi)有字符的轉(zhuǎn)換結(jié)果。
signed char
short
int
long
long long
intmax_t
簽 size_t
ptrdiff_t
N / A o
將無(wú)符號(hào)整數(shù)轉(zhuǎn)換為八進(jìn)制表示oooo。 精度指定出現(xiàn)的最小位數(shù)。默認(rèn)的精度是1
。如果轉(zhuǎn)換后的值和精度都是0
沒(méi)有字符的轉(zhuǎn)換結(jié)果。在替代實(shí)現(xiàn)中,如果需要,可以增加精度以寫(xiě)入一個(gè)前導(dǎo)零。在這種情況下,如果轉(zhuǎn)換值和精度都是0
,0
寫(xiě)入單個(gè)。
unsigned char
unsigned short
unsigned int
unsigned long
unsigned long long
uintmax_t
size_t
未簽名的版本 ptrdiff_t
N/A x
X
將無(wú)符號(hào)整數(shù)轉(zhuǎn)換為十六進(jìn)制表示hhhh。使用x
轉(zhuǎn)換字母abcdef
。
使用X
轉(zhuǎn)換字母ABCDEF
。
精度指定出現(xiàn)的最小位數(shù)。默認(rèn)的精度是1
。如果轉(zhuǎn)換后的值和精度都是0
沒(méi)有字符的轉(zhuǎn)換結(jié)果。在替代實(shí)現(xiàn)中, 0x
或者0X
如果轉(zhuǎn)換后的值不為零,則將其作為結(jié)果的前綴。
N / A u
將無(wú)符號(hào)整數(shù)轉(zhuǎn)換為十進(jìn)制表示形式dddd。 精度指定出現(xiàn)的最小位數(shù)。默認(rèn)的精度是1
。如果轉(zhuǎn)換后的值和精度都是0
沒(méi)有字符的轉(zhuǎn)換結(jié)果。
N/A f
F
將浮點(diǎn)數(shù)轉(zhuǎn)換為樣式-ddd.ddd中的小數(shù)表示法。 精度指定小數(shù)點(diǎn)后面出現(xiàn)的最小位數(shù)。默認(rèn)的精度是6
。在替代實(shí)現(xiàn)中,即使沒(méi)有數(shù)字跟隨,小數(shù)點(diǎn)字符也會(huì)被寫(xiě)入。對(duì)于無(wú)窮大和非數(shù)字轉(zhuǎn)換風(fēng)格,請(qǐng)參閱注釋。
N/A N/A double
`double` (C99)
N/A N/A N/A N/A long double
`e`
E
將浮點(diǎn)數(shù)轉(zhuǎn)換為十進(jìn)制指數(shù)符號(hào)。對(duì)于e
轉(zhuǎn)換樣式,使用-d.ddd e
±dd。
對(duì)于E
轉(zhuǎn)換樣式,使用-d.ddd E
±dd。
指數(shù)至少包含兩位數(shù)字,只有在必要時(shí)才使用更多數(shù)字。如果值是0
,指數(shù)也是0
。精度指定小數(shù)點(diǎn)后面出現(xiàn)的最小位數(shù)。默認(rèn)的精度是6
。在替代實(shí)現(xiàn)中,即使沒(méi)有數(shù)字跟隨,小數(shù)點(diǎn)字符也會(huì)被寫(xiě)入。對(duì)于無(wú)窮大和非數(shù)字轉(zhuǎn)換風(fēng)格,請(qǐng)參閱注釋。
N/A N/A N/A N/A N/A N/A a
A
(C99).
將浮點(diǎn)數(shù)轉(zhuǎn)換為十六進(jìn)制指數(shù)表示法。對(duì)于a
轉(zhuǎn)換樣式- 使用0x
h.hhh p
±d。
對(duì)于A
轉(zhuǎn)換樣式- 使用0X
h.hhh P
±d。
0
如果參數(shù)不是標(biāo)準(zhǔn)化的浮點(diǎn)值,則第一個(gè)十六進(jìn)制數(shù)字是。如果值是0
,指數(shù)也是0
。精度指定小數(shù)點(diǎn)后面出現(xiàn)的最小位數(shù)。默認(rèn)精度足以精確表示值。在替代實(shí)現(xiàn)中,即使沒(méi)有數(shù)字跟隨,小數(shù)點(diǎn)字符也會(huì)被寫(xiě)入。對(duì)于無(wú)窮大和非數(shù)字轉(zhuǎn)換風(fēng)格,請(qǐng)參閱注釋。
N/A N/A N/A N/A N/A N/A g
G
根據(jù)值和精度 將浮點(diǎn)數(shù)轉(zhuǎn)換為十進(jìn)制或十進(jìn)制指數(shù)符號(hào)。對(duì)于風(fēng)格轉(zhuǎn)換的轉(zhuǎn)換與風(fēng)格或?qū)⒈粓?zhí)行。gef
對(duì)于G
風(fēng)格轉(zhuǎn)換的轉(zhuǎn)換與風(fēng)格E
或F
將被執(zhí)行。
讓P
等于精度如果非零,6
如果沒(méi)有指定精度,或者1
如果精度是0
。然后,如果具有樣式的轉(zhuǎn)換E
將具有以下指數(shù)X
:
如果P> X≥-4,轉(zhuǎn)換是用式f
或F
和精度P - 1 - X。
否則,轉(zhuǎn)換采用樣式e
或E
精度P - 1。
除非請(qǐng)求替代表示,否則尾隨零將被刪除,如果沒(méi)有剩余小數(shù)部分,小數(shù)點(diǎn)字符也會(huì)被刪除。對(duì)于無(wú)窮大和非數(shù)字轉(zhuǎn)換風(fēng)格,請(qǐng)參閱注釋。
不適用/不適用/不適用/不適用/ n
返回此函數(shù)迄今為止寫(xiě)入的字符數(shù)。結(jié)果寫(xiě)入參數(shù)指向的值。規(guī)范可能不包含任何標(biāo)志,字段寬度或精度。
signed char*
short*
int*
long*
long long*
intmax_t*
signed size_t*
ptrdiff_t*
N / A p
寫(xiě)入一個(gè)實(shí)現(xiàn)定義的字符序列來(lái)定義一個(gè)指針。不適用不適用不適用不適用不適用不適用 void*
浮點(diǎn)轉(zhuǎn)換函數(shù)將無(wú)窮大轉(zhuǎn)換為inf
或infinity
。使用哪一個(gè)是實(shí)現(xiàn)定義的。
非數(shù)字轉(zhuǎn)換為nan
或。使用哪一個(gè)是實(shí)現(xiàn)定義的。nan(char_sequence)
該轉(zhuǎn)換F
,E
,G
,A
輸出INF
,INFINITY
,NAN
來(lái)代替。
即使%c
需要int
參數(shù),通過(guò)char
調(diào)用可變參數(shù)函數(shù)時(shí)發(fā)生的整數(shù)提升也是安全的。
對(duì)于固定寬度的字符類型(正確的轉(zhuǎn)換規(guī)格int8_t
<inttypes.h>還(雖然,等等)都在頭定義PRIdMAX
,PRIuMAX
等是同義詞%jd
,%ju
等)。
內(nèi)存寫(xiě)入轉(zhuǎn)換說(shuō)明符%n
是安全漏洞的常見(jiàn)目標(biāo),其中格式字符串取決于用戶輸入,并且不受邊界檢查printf_s
函數(shù)族的支持。
每個(gè)轉(zhuǎn)換說(shuō)明符的操作之后都有一個(gè)序列點(diǎn); 這允許將多個(gè)%n
結(jié)果存儲(chǔ)在相同的變量中,或者作為邊緣情況,%n
在同一個(gè)調(diào)用中打印由較早修改的字符串。
如果轉(zhuǎn)換規(guī)范無(wú)效,則行為未定義。
... - arguments specifying data to print
1,2)如果發(fā)生錯(cuò)誤,則成功寫(xiě)入寬字符數(shù)或?qū)懭胴?fù)值。
3)如果編碼錯(cuò)誤發(fā)生或者如果要生成的字符數(shù)等于或大于size
(包括當(dāng)size
為零時(shí)),則寫(xiě)入的寬字符的數(shù)目(不包括終止空寬字符)如果成功或負(fù)值。
4,5)如果發(fā)生錯(cuò)誤,則成功寫(xiě)入寬字符數(shù)或?qū)懭胴?fù)值。
6)寫(xiě)入的寬字符數(shù)(不包括終止空值)buffer
。返回編碼錯(cuò)誤和溢出時(shí)的負(fù)值。對(duì)所有其他錯(cuò)誤返回零。
7)的,將已經(jīng)寫(xiě)入寬字符(不包括終止空)數(shù)量buffer
已經(jīng)bufsz
如果發(fā)生錯(cuò)誤,已經(jīng)足夠大時(shí),或?yàn)樨?fù)值。(也就是說(shuō),只有當(dāng)回報(bào)是非負(fù)的且小于時(shí),寫(xiě)才能成功并且完成bufsz
)
雖然窄字符串提供snprintf
了可以確定所需的輸出緩沖區(qū)大小的可能性,但對(duì)于寬字符串沒(méi)有等效(直到C11的snwprintf_s),并且為了確定緩沖區(qū)大小,程序可能需要調(diào)用swprintf
,檢查結(jié)果值,并重新分配一個(gè)更大的緩沖區(qū),再次嘗試直到成功。
snwprintf_s
不同swprintf_s
,將截?cái)嘟Y(jié)果以適應(yīng)指向的數(shù)組buffer
,即使截?cái)啾淮蠖鄶?shù)邊界檢查函數(shù)視為錯(cuò)誤。
#include <locale.h>#include <wchar.h> int main(void){ char narrow_str[] = "z\u00df\u6c34\U0001f34c"; // or "z?水?" // or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9f\x8d\x8c"; wchar_t warr[29]; // the expected string is 28 characters plus 1 null terminator setlocale(LC_ALL, "en_US.utf8"); swprintf(warr, sizeof warr/sizeof *warr, L"Converted from UTF-8: '%s'", narrow_str); wprintf(L"%ls\n", warr);}
輸出:
Converted from UTF-8: 'z?水?'
C11標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
7.29.2.1 fwprintf函數(shù)(p:403-410)
7.29.2.3 swprintf函數(shù)(p:416)
7.29.2.11 wprintf函數(shù)(p:421)
K.3.9.1.1 fwprintf_s函數(shù)(p:628)
K.3.9.1.4 swprintf_s函數(shù)(p:630-631)
K.3.9.1.13 wprintf_s函數(shù)(p:637-638)
C99標(biāo)準(zhǔn)(ISO / IEC 9899:1999):
7.24.2.1 fwprintf函數(shù)(p:349-356)
7.24.2.3 swprintf函數(shù)(p:362)
7.24.2.11 wprintf函數(shù)(p:366)