?
This document uses PHP Chinese website manual Release
在頭文件<stdio.h>中定義 | ||
---|---|---|
(1) | ||
int printf(const char * format,...); | (直到C99) | |
int printf(const char * restrict format,...); | (自C99以來) | |
(2) | ||
int fprintf(FILE * stream,const char * format,...); | (直到C99) | |
int fprintf(FILE * restrict stream,const char * restrict format,...); | (自C99以來) | |
(3) | ||
int sprintf(char * buffer,const char * format,...); | (直到C99) | |
int sprintf(char * restrict buffer,const char * restrict format,...); | (自C99以來) | |
int snprintf(char *限制緩沖區(qū),int bufsz,const char *限制格式,...); | (4) | (自C99以來) |
int printf_s(const char * restrict format,...); | (5) | (自C11以來) |
int fprintf_s(FILE * restrict stream,const char * restrict format,...); | (6) | (自C11以來) |
int sprintf_s(char * restrict buffer,rsize_t bufsz,const char * restrict format,...); | (7) | (自C11以來) |
int snprintf_s(char * restrict buffer,rsize_t bufsz,const char * restrict format,...); | (8) | (自C11以來) |
從給定的位置加載數(shù)據(jù),將它們轉換為字符串等價物并將結果寫入各種接收器。
1)將結果寫入輸出流stdout
。
2)將結果寫入輸出流stream
。
3)將結果寫入字符串buffer
。如果要寫入的字符串(加上終止的空字符)超過了指向的數(shù)組的大小,則行為未定義buffer
。
4)將結果寫入字符串buffer
。最多bufsz
- 寫入1個字符。結果字符串將以空字符結尾,除非bufsz
為零。如果bufsz
為零,則不會寫入任何內(nèi)容,并且buffer
可能是空指針,但返回值(將寫入的字節(jié)數(shù))仍然會計算并返回。
5-8)與(1-4)相同,只是在運行時檢測到以下錯誤并調(diào)用當前安裝的約束處理函數(shù):
轉換說明符%n
存在于format
任何對應的參數(shù)%s
都是空指針
format
或者buffer
是空指針
bufsz
是零或大于 RSIZE_MAX
編碼錯誤出現(xiàn)在任何字符串和字符轉換說明符中
(sprintf_s
僅限于),要存儲的字符串buffer
(包括結尾空值)將被超出bufsz
由于所有的邊界檢查功能,printf_s
,fprintf_s
,sprintf_s
,和snrintf_s
僅保證可供如果__STDC_LIB_EXT1__
由實現(xiàn)所定義,并且如果用戶定義__STDC_WANT_LIB_EXT1__
的整數(shù)常數(shù)1
,包括之前<stdio.h>
。
流 | - | 輸出文件流寫入 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
緩沖 | - | 指向要寫入的字符串的指針 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bufsz | - | 最多bufsz - 可能會寫入1個字符,再加上空終止符 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
格式 | - | 指向以空字符結尾的多字節(jié)字符串的指針,指定如何解釋數(shù)據(jù)。格式字符串由普通的多字節(jié)字符(%除外)組成,它們被原樣復制到輸出流和轉換規(guī)范中。每個轉換規(guī)范具有以下格式:介紹%字符(可選)一個或多個標志,用于修改轉換的行為: - :轉換結果在字段內(nèi)左對齊(默認情況下,它是右對齊的)+ :帶符號轉換的符號總是作為轉換結果的前綴(默認情況下,只有當結果為負時,結果才以負值開頭)space:如果帶符號轉換的結果不是以符號字符開頭,或者是空的,空間是預先考慮的結果。如果存在+標志,則忽略它。#:執(zhí)行轉換的替代形式。請參閱下表以了解確切的效果,否則行為未定義。0:對于整數(shù)和浮點數(shù)轉換,前導零用于填充字段而不是空格字符。對于整數(shù),如果明確指定了精度,它將被忽略。對于使用此標志的其他轉換會導致未定義的行為。如果 - 標志存在,它將被忽略。(可選)整數(shù)值或*指定最小字段寬度。如果需要,結果會填充空格字符(默認情況下),右側對齊時填充空白字符,左側填充右側填充。在使用*的情況下,寬度由類型為int的附加參數(shù)指定。如果參數(shù)的值是負值,那么結果是指定 - 標志和正字段寬度。(注意:這是最小寬度:該值從不被截斷。)(可選)。后跟整數(shù)或*,或者兩者都不指定轉換的精度。在使用*的情況下,精度由類型為int的附加參數(shù)指定。如果這個參數(shù)的值是負數(shù),它將被忽略。如果既不使用數(shù)字也不使用*,則精度取為零。請參閱下表以了解精確度的確切影響。(可選)長度修飾符,用于指定參數(shù)轉換格式說明符的大小以下格式說明符可用:轉換說明符說明參數(shù)類型長度修飾符hh(C99)。h(無)ll(C99)。j(C99)。z(C99)。t(C99)。L%寫文字%。完整的轉換規(guī)范必須是%%。不適用不適用不適用不適用不適用不適用不適用不適用不適用單個字符。該參數(shù)首先轉換為無符號字符。如果使用l修飾符,則首先將參數(shù)轉換為字符串,就好像通過具有wchar_t2參數(shù)的%ls一樣。N / AN / A int wint_t N / AN / AN / AN / AN / A s寫入字符串參數(shù)必須是指向字符數(shù)組的初始元素的指針。Precision指定要寫入的最大字節(jié)數(shù)。如果未指定Precision,則將每個字節(jié)寫入并不包括第一個空終止符。如果使用l說明符,則參數(shù)必須是指向wchar_t數(shù)組的初始元素的指針,該數(shù)組轉換為char數(shù)組,就好像通過調(diào)用具有零初始化轉換狀態(tài)的wcrtomb一樣。N / AN / A char * wchar_t * N / AN / AN / AN / AN / A di將有符號整數(shù)轉換為十進制表示形式-dddd。精度指定出現(xiàn)的最小位數(shù)。默認精度為1.如果轉換值和精度均為0,則轉換不會生成任何字符。signed char short int long long long intmax_t signed size_t ptrdiff_t不適用o將無符號整數(shù)轉換為八進制表示oooo。精度指定出現(xiàn)的最小位數(shù)。默認精度為1.如果轉換值和精度均為0,則轉換不會生成任何字符。在替代實現(xiàn)中,如果需要,可以增加精度以寫入一個前導零。在這種情況下,如果轉換值和精度均為0,則寫入單個0。unsigned char unsigned short unsigned int unsigned long unsigned long long uintmax_t size_t ptrdiff_t的無符號版本不適用x X將無符號整數(shù)轉換為十六進制表示形式hhhh。對于x轉換字母abcdef被使用。對于X轉換字母使用ABCDEF。精度指定出現(xiàn)的最小位數(shù)。默認精度為1.如果轉換值和精度均為0,則轉換不會生成任何字符。在替代實現(xiàn)中,如果轉換后的值為非零值,則0x或0X將作為結果的前綴。不適用u將無符號整數(shù)轉換為十進制表示形式dddd。精度指定出現(xiàn)的最小位數(shù)。默認精度為1.如果轉換值和精度均為0,則轉換不會生成任何字符。N / A f F將浮點數(shù)轉換為樣式-ddd.ddd中的十進制表示法。精度指定小數(shù)點后面出現(xiàn)的最小位數(shù)。默認精度為6.在替代實現(xiàn)中,即使沒有數(shù)字跟隨,小數(shù)點字符也會被寫入。對于無限和非數(shù)字轉換風格,請參閱注釋。N / AN / A double double(C99)N / AN / AN / AN / A long double e E將浮點數(shù)轉換為十進制指數(shù)表示法。對于e轉換樣式,使用-d.ddde±dd。對于E轉換樣式,使用-d.dddE±dd。指數(shù)至少包含兩位數(shù)字,只有在必要時才使用更多數(shù)字。如果該值為0,則指數(shù)也為0。精度指定小數(shù)點后面出現(xiàn)的最小位數(shù)。默認精度為6.在替代實現(xiàn)中,即使沒有數(shù)字跟隨,小數(shù)點字符也會被寫入。對于無限和非數(shù)字轉換風格,請參閱注釋。不適用不適用不適用不適用不適用不適用(C99)。將浮點數(shù)轉換為十六進制指數(shù)表示法。對于轉換類型,使用-0xh.hhhp±d。對于A轉換樣式,使用-0Xh.hhhP±d。如果參數(shù)不是標準化的浮點值,則第一個十六進制數(shù)字為0。如果該值為0,則指數(shù)也為0。精度指定小數(shù)點后面出現(xiàn)的最小位數(shù)。默認精度足以精確表示值。在替代實現(xiàn)中,即使沒有數(shù)字跟隨,小數(shù)點字符也會被寫入。對于無限和非數(shù)字轉換風格,請參閱注釋。不可用/不可用/不可用/不可用/ g G根據(jù)值和精度將浮點數(shù)轉換為十進制或十進制指數(shù)符號。對于使用樣式e或f的g轉換樣式轉換將被執(zhí)行。對于G轉換樣式,將使用樣式E或F進行轉換。假設P等于非零的精度,如果未指定精度,則等于6;如果精度為0,則等于1。然后,如果具有樣式E的轉換將具有X的指數(shù):如果P> X≥-4,則轉換具有樣式f或F和精度P-1-X。否則,轉換具有樣式e或E以及精度P - 1.除非要求替代表示,否則尾隨零將被刪除,如果沒有剩余小數(shù)部分,小數(shù)點字符也會被刪除。對于無限和非數(shù)字轉換風格,請參閱注釋。不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用 結果寫入?yún)?shù)指向的值。規(guī)范可能不包含任何標志,字段寬度或精度。signed char * short * int * long * long long * intmax_t * signed size_t * ptrdiff_t * N / A p寫一個實現(xiàn)定義的字符序列來定義一個指針。不適用不適用不適用不適用不適用不適用不適用浮點轉換函數(shù)將無窮大轉換為inf或無窮大。使用哪一個是實現(xiàn)定義的。非數(shù)字轉換為南或南(char_sequence)。使用哪一個是實現(xiàn)定義的。轉換F,E,G,A代替輸出INF,INFINITY,NAN。盡管%c需要int參數(shù),但因為在調(diào)用可變參數(shù)函數(shù)時發(fā)生整數(shù)提升,所以傳遞char是安全的。固定寬度字符類型(int8_t等)的正確轉換規(guī)范在標頭<inttypes.h>中定義(盡管PRIdMAX,PRIuMAX等與%jd,%ju等同義)。內(nèi)存寫入轉換說明符%n是格式字符串依賴用戶輸入并且不受邊界檢查的printf_s系列函數(shù)支持的安全漏洞的常見目標。每個轉換說明符的操作之后都有一個序列點; 這允許在同一個變量中存儲多個%n個結果,或者,作為邊界案例,在同一個調(diào)用中打印由較早的%n修改的字符串。如果轉換規(guī)范無效,則行為未定義。由于在調(diào)用可變參數(shù)函數(shù)時發(fā)生整數(shù)提升,所以傳遞char是安全的。固定寬度字符類型(int8_t等)的正確轉換規(guī)范在標頭<inttypes.h>中定義(盡管PRIdMAX,PRIuMAX等與%jd,%ju等同義)。內(nèi)存寫入轉換說明符%n是格式字符串依賴用戶輸入并且不受邊界檢查的printf_s系列函數(shù)支持的安全漏洞的常見目標。每個轉換說明符的操作之后都有一個序列點; 這允許在同一個變量中存儲多個%n個結果,或者,作為邊界案例,在同一個調(diào)用中打印由較早的%n修改的字符串。如果轉換規(guī)范無效,則行為未定義。由于在調(diào)用可變參數(shù)函數(shù)時發(fā)生整數(shù)提升,所以傳遞char是安全的。固定寬度字符類型(int8_t等)的正確轉換規(guī)范在標頭<inttypes.h>中定義(盡管PRIdMAX,PRIuMAX等與%jd,%ju等同義)。內(nèi)存寫入轉換說明符%n是格式字符串依賴用戶輸入并且不受邊界檢查的printf_s系列函數(shù)支持的安全漏洞的常見目標。每個轉換說明符的操作之后都有一個序列點; 這允許在同一個變量中存儲多個%n個結果,或者,作為邊界案例,在同一個調(diào)用中打印由較早的%n修改的字符串。如果轉換規(guī)范無效,則行為未定義。 | 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 unsigned char. If the l modifier is used, the argument is first converted to a character string as if by %ls with a wchar_t2 argument. | 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 an array of characters. Precision specifies the maximum number of bytes to be written. If Precision is not specified, writes every byte 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, which is converted to char array as if by a call to wcrtomb with zero-initialized conversion state. | 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 unsigned char. If the l modifier is used, the argument is first converted to a character string as if by %ls with a wchar_t2 argument. | 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 an array of characters. Precision specifies the maximum number of bytes to be written. If Precision is not specified, writes every byte 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, which is converted to char array as if by a call to wcrtomb with zero-initialized conversion state. | 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). | 將浮點數(shù)轉換為十六進制指數(shù)表示法。對于轉換類型,使用-0xh.hhhp±d。對于A轉換樣式,使用-0Xh.hhhP±d。如果參數(shù)不是標準化的浮點值,則第一個十六進制數(shù)字為0。如果該值為0,則指數(shù)也為0。精度指定小數(shù)點后面出現(xiàn)的最小位數(shù)。默認精度足以精確表示值。在替代實現(xiàn)中,即使沒有數(shù)字跟隨它,也會寫入小數(shù)點字符。對于無窮大和非數(shù)字轉換風格,請參閱注釋。 | N / A | N / A | N / A | N / A | N / A | N / A | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
g G | 根據(jù)值和精度將浮點數(shù)轉換為十進制或十進制指數(shù)符號。對于使用樣式e或f的g轉換樣式轉換將被執(zhí)行。對于G轉換樣式,將使用樣式E或F進行轉換。假設P等于非零的精度,如果未指定精度,則等于6;如果精度為0,則等于1。然后,如果具有樣式E的轉換將具有X的指數(shù):如果P> X≥-4,則轉換具有樣式f或F和精度P-1-X。否則,轉換具有樣式e或E以及精度P - 1.除非要求替代表示,否則尾隨零將被刪除,如果沒有剩余小數(shù)部分,小數(shù)點字符也會被刪除。對于無窮大和非數(shù)字轉換風格,請參閱注釋。 | N / A | N / A | N / A | N / A | N / A | N / A | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
? | 將此調(diào)用到目前為止寫入的字符數(shù)返回給該函數(shù)。結果寫入?yún)?shù)指向的值。規(guī)范可能不包含任何標志,字段寬度或精度。 | 簽名字符* | 短* | INT * | long* | 很長* | *將intmax_t | 簽名size_t * | ptrdiff_t的* | N / A | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
p | 寫一個實現(xiàn)定義的字符序列來定義一個指針。 | N / A | N / A | 無效* | N / A | N / A | N / A | N / A | N / A | N / A | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
... | - | 指定要打印的數(shù)據(jù)的參數(shù) |
介紹%
人物
(可選)一個或多個修改轉換行為的標志:
-
:轉換的結果在字段內(nèi)左對齊(默認情況下它是右對齊的)
+
:帶符號轉換的符號總是預設為轉換結果的前綴(默認情況下結果前面為減號,僅當它為負值時)
空格:如果簽名轉換的結果不是以符號字符開頭,或者是空的,則空格會預設為結果。如果+
存在標志,則忽略它。
#
:執(zhí)行轉換的替代形式。請參閱下表以了解確切的效果,否則行為未定義。
0
:對于整數(shù)和浮點數(shù)轉換,前導零用于填充字段而不是空格字符。對于整數(shù),如果明確指定了精度,它將被忽略。對于使用此標志的其他轉換會導致未定義的行為。如果-
存在標志,則忽略它。
(可選)整數(shù)值或*
指定最小字段寬度。如果需要,結果會填充空格字符(默認情況下),右側對齊時填充空白字符,左側填充右側填充。在使用的情況下*
,寬度由類型的附加參數(shù)指定int
。如果參數(shù)的值是負數(shù),則結果是-
指定的標志和正的字段寬度。(注意:這是最小寬度:該值從不被截斷。)
(可選).
后面跟隨整數(shù)或者*
或者既不指定轉換的精度。在使用的情況下*
,精度由類型的附加參數(shù)指定int
。如果這個參數(shù)的值是負數(shù),它將被忽略。如果既不使用數(shù)字也不*
使用,則精度取為零。請參閱下表以了解精確度的確切影響。
(可選)長度修飾符,用于指定參數(shù)的大小
轉換格式說明符
以下格式說明符可用:
Conversion
說明符說明參數(shù)類型長度修飾符 hh
(C99)。
h
(none) l
ll
(C99).
j
(C99).
z
(C99).
t
(C99).
L
%
寫文字%
。完整的轉換規(guī)范必須是%%
。N / AN / AN / AN / AN / AN / AN / AN / AN / A c
寫入單個字符。該論點首先轉換為unsigned char
。如果使用了l修飾符,則首先將參數(shù)轉換為字符串,就像通過具有參數(shù)的%ls一樣wchar_t[2]
。
N/A N/A int
wint_t
N / AN / AN / AN / AN / A s
寫入字符串參數(shù)必須是指向字符數(shù)組的初始元素的指針。Precision指定要寫入的最大字節(jié)數(shù)。如果未指定Precision,則將每個字節(jié)寫入并不包括第一個空終止符。如果使用了l說明符,則參數(shù)必須是指向數(shù)組初始元素的指針wchar_t
,它將轉換為char數(shù)組,就像通過調(diào)用wcrtomb
具有零初始化轉換狀態(tài)一樣。
N/A N/A char*
wchar_t*
N/A N/A N/A N/A N/A d
i
將有符號的整數(shù)轉換為十進制表示形式-dddd。 精度指定出現(xiàn)的最小位數(shù)。默認的精度是1
。
如果轉換后的值和精度都是0
沒有字符的轉換結果。
signed char
short
int
long
long long
intmax_t
signed size_t
ptrdiff_t
N / A o
將無符號整數(shù)轉換為八進制表示oooo。 精度指定出現(xiàn)的最小位數(shù)。默認的精度是1
。如果轉換后的值和精度都是0
沒有字符的轉換結果。在替代實現(xiàn)中,如果需要,可以增加精度以寫入一個前導零。在這種情況下,如果轉換值和精度都是0
,0
寫入單個。
unsigned char
unsigned short
unsigned int
unsigned long
unsigned long long
uintmax_t
size_t
未簽名的版本 ptrdiff_t
N/A x
X
將無符號整數(shù)轉換為十六進制表示hhhh。使用x
轉換字母abcdef
。
For the X
conversion letters ABCDEF
are used.
精度指定出現(xiàn)的最小位數(shù)。默認的精度是1
。如果轉換后的值和精度都是0
沒有字符的轉換結果。在替代實現(xiàn)中, 0x
或者0X
如果轉換后的值不為零,則將其作為結果的前綴。
N / A u
將無符號整數(shù)轉換為十進制表示形式dddd。 精度指定出現(xiàn)的最小位數(shù)。默認的精度是1
。如果轉換后的值和精度都是0
沒有字符的轉換結果。
N/A f
F
將浮點數(shù)轉換為樣式-ddd.ddd中的小數(shù)表示法。 精度指定小數(shù)點后面出現(xiàn)的最小位數(shù)。默認的精度是6
。在替代實現(xiàn)中,即使沒有數(shù)字跟隨,小數(shù)點字符也會被寫入。對于無窮大和非數(shù)字轉換風格,請參閱注釋。
N/A N/A double
`double` (C99)
N/A N/A N/A N/A long double
`e`
E
將浮點數(shù)轉換為十進制指數(shù)符號。對于e
轉換樣式,使用-d.ddd e
±dd。
對于E
轉換樣式,使用-d.ddd E
±dd。
指數(shù)至少包含兩位數(shù)字,只有在必要時才使用更多數(shù)字。如果值是0
,指數(shù)也是0
。精度指定小數(shù)點后面出現(xiàn)的最小位數(shù)。默認的精度是6
。在替代實現(xiàn)中,即使沒有數(shù)字跟隨,小數(shù)點字符也會被寫入。對于無窮大和非數(shù)字轉換風格,請參閱注釋。
N/A N/A N/A N/A N/A N/A a
A
(C99).
將浮點數(shù)轉換為十六進制指數(shù)表示法。對于a
轉換樣式- 使用0x
h.hhh p
±d。
對于A
轉換樣式- 使用0X
h.hhh P
±d。
0
如果參數(shù)不是標準化的浮點值,則第一個十六進制數(shù)字是。如果值是0
,指數(shù)也是0
。精度指定小數(shù)點后面出現(xiàn)的最小位數(shù)。默認精度足以精確表示值。在替代實現(xiàn)中,即使沒有數(shù)字跟隨,小數(shù)點字符也會被寫入。對于無窮大和非數(shù)字轉換風格,請參閱注釋。
N/A N/A N/A N/A N/A N/A g
G
根據(jù)值和精度 將浮點數(shù)轉換為十進制或十進制指數(shù)符號。對于風格轉換的轉換與風格或?qū)⒈粓?zhí)行。gef
對于G
風格轉換的轉換與風格E
或F
將被執(zhí)行。
讓P
等于精度如果非零,6
如果沒有指定精度,或者1
如果精度是0
。然后,如果具有樣式的轉換E
將具有以下指數(shù)X
:
如果P> X≥-4,轉換是用式f
或F
和精度P - 1 - X。
否則,轉換采用樣式e
或E
精度P - 1。
除非請求替代表示,否則尾隨零將被刪除,如果沒有剩余小數(shù)部分,小數(shù)點字符也會被刪除。對于無窮大和非數(shù)字轉換風格,請參閱注釋。
不適用/不適用/不適用/不適用/ n
返回此函數(shù)迄今為止寫入的字符數(shù)。結果寫入參數(shù)指向的值。規(guī)范可能不包含任何標志,字段寬度或精度。
signed char*
short*
int*
long*
long long*
intmax_t*
signed size_t*
ptrdiff_t*
N / A p
寫入一個實現(xiàn)定義的字符序列來定義一個指針。不適用不適用不適用不適用不適用不適用 void*
浮點轉換函數(shù)將無窮大轉換為inf
或infinity
。使用哪一個是實現(xiàn)定義的。
非數(shù)字轉換為nan
或。使用哪一個是實現(xiàn)定義的。nan(char_sequence)
該轉換F
,E
,G
,A
輸出INF
,INFINITY
,NAN
來代替。
即使%c
需要int
參數(shù),通過char
調(diào)用可變參數(shù)函數(shù)時發(fā)生的整數(shù)提升也是安全的。
對于固定寬度的字符類型(正確的轉換規(guī)格int8_t
<inttypes.h>還(雖然,等等)都在頭定義PRIdMAX
,PRIuMAX
等是同義詞%jd
,%ju
等)。
內(nèi)存寫入轉換說明符%n
是安全漏洞的常見目標,其中格式字符串取決于用戶輸入,并且不受邊界檢查printf_s
函數(shù)族的支持。
每個轉換說明符的操作之后都有一個序列點; 這允許將多個%n
結果存儲在相同的變量中,或者作為邊緣情況,%n
在同一個調(diào)用中打印由較早修改的字符串。
如果轉換規(guī)范無效,則行為未定義。
... - arguments specifying data to print
1,2)發(fā)送到輸出流的字符數(shù)或負值(如果發(fā)生輸出錯誤或編碼錯誤(用于字符串和字符轉換說明符))
3)寫入的字符數(shù)buffer
(不包括終止空字符);如果發(fā)生編碼錯誤(用于字符串和字符轉換說明符),則返回負值
4)buffer
如果bufsz
被忽略,將被寫入的字符數(shù)(不包括終止空字符),或者如果編碼錯誤(對于字符串和字符轉換說明符)發(fā)生,則為負值
5,6)傳輸?shù)捷敵隽鞯淖址麛?shù)或負值(如果發(fā)生輸出錯誤,運行時間約束違規(guī)錯誤或編碼錯誤)。
7)寫入的字符數(shù)buffer
,不包括空字符(只要buffer
不是空指針,bufsz
并且不為零且不大于RSIZE_MAX
),則不計入空字符,或者在運行時約束違規(guī)時為零,編碼錯誤為負值
8)不包括終止空字符的字符數(shù)(只要buffer
不是空指針并且bufsz
不為零且不大于RSIZE_MAX
),buffer
如果bufsz
被忽略,將被寫入的字符數(shù)或者如果運行時約束違規(guī)或編碼錯誤發(fā)生
C標準和POSIX指定sprintf
當參數(shù)與目標緩沖區(qū)重疊時,其行為及其變體未定義。例:
sprintf(dst, "%s and %s", dst, t); // <- broken: undefined behavior
POSIX指定該errno
設置上的錯誤。它還指定了額外的轉換規(guī)范,最顯著的是支持參數(shù)重新排序(n$
緊接在%
指示n
'th參數(shù)之后)。
snprintf
使用零bufsz
和空指針調(diào)用buffer
對于確定包含輸出所需的緩沖區(qū)大小很有用:
const char *fmt = "sqrt(2) = %f";int sz = snprintf(NULL, 0, fmt, sqrt(2));char buf[sz + 1]; // note +1 for terminating null bytesnprintf(buf, sizeof buf, fmt, sqrt(2));
snprintf_s
,就像snprintf
,但不像sprintf_s
,會截斷輸出以適應bufsz-1
。
#include <stdio.h> int main(void){ printf("Strings:\n"); const char* s = "Hello"; printf("\t.%10s.\n\t.%-10s.\n\t.%*s.\n", s, s, 10, s); printf("Characters:\t%c %%\n", 65); printf("Integers\n"); printf("Decimal:\t%i %d %.6i %i %.0i %+i %u\n", 1, 2, 3, 0, 0, 4, -1); printf("Hexadecimal:\t%x %x %X %#x\n", 5, 10, 10, 6); printf("Octal:\t%o %#o %#o\n", 10, 10, 4); printf("Floating point\n"); printf("Rounding:\t%f %.0f %.32f\n", 1.5, 1.5, 1.3); printf("Padding:\t%05.2f %.2f %5.2f\n", 1.5, 1.5, 1.5); printf("Scientific:\t%E %e\n", 1.5, 1.5); printf("Hexadecimal:\t%a %A\n", 1.5, 1.5);}
輸出:
Strings: . Hello. .Hello . . Hello.Characters: A %Integers Decimal: 1 2 000003 0 +4 4294967295Hexadecimal: 5 a A 0x6Octal: 12 012 04Floating point Rounding: 1.500000 2 1.30000000000000004440892098500626Padding: 01.50 1.50 1.50Scientific: 1.500000E+00 1.500000e+00Hexadecimal: 0x1.8p+0 0X1.8P+0
C11標準(ISO / IEC 9899:2011):
7.21.6.1 fprintf函數(shù)(p:309-316)
7.21.6.3 printf函數(shù)(p:324)
7.21.6.5 snprintf函數(shù)(p:325)
7.21.6.6 sprintf函數(shù)(p:325-326)
K.3.5.3.1 fprintf_s函數(shù)(p:591)
K.3.5.3.3 printf_s函數(shù)(p:593-594)
K.3.5.3.5 snprintf_s函數(shù)(p:594-595)
K.3.5.3.6 sprintf_s函數(shù)(p:595-596)
C99標準(ISO / IEC 9899:1999):
7.19.6.1 fprintf函數(shù)(p:274-282)
7.19.6.3 printf函數(shù)(p:290)
7.19.6.5 snprintf函數(shù)(p:290-291)
7.19.6.6 sprintf函數(shù)(p:291)
C89 / C90標準(ISO / IEC 9899:1990):
4.9.6.1 fprintf函數(shù)
4.9.6.3 printf函數(shù)
4.9.6.5 sprintf函數(shù)