?
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ù),將它們轉(zhuǎn)換為字符串等價物并將結(jié)果寫入各種接收器。
1)將結(jié)果寫入輸出流stdout
。
2)將結(jié)果寫入輸出流stream
。
3)將結(jié)果寫入字符串buffer
。如果要寫入的字符串(加上終止的空字符)超過了指向的數(shù)組的大小,則行為未定義buffer
。
4)將結(jié)果寫入字符串buffer
。最多bufsz
- 寫入1個字符。結(jié)果字符串將以空字符結(jié)尾,除非bufsz
為零。如果bufsz
為零,則不會寫入任何內(nèi)容,并且buffer
可能是空指針,但返回值(將寫入的字節(jié)數(shù))仍然會計算并返回。
5-8)與(1-4)相同,只是在運行時檢測到以下錯誤并調(diào)用當前安裝的約束處理函數(shù):
轉(zhuǎn)換說明符%n
存在于format
任何對應的參數(shù)%s
都是空指針
format
或者buffer
是空指針
bufsz
是零或大于 RSIZE_MAX
編碼錯誤出現(xiàn)在任何字符串和字符轉(zhuǎn)換說明符中
(sprintf_s
僅限于),要存儲的字符串buffer
(包括結(jié)尾空值)將被超出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個字符,再加上空終止符 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
格式 | - | pointer to a null-terminated multibyte string specifying how to interpret the data. The format string consists of ordinary multibyte characters (except %), which are copied unchanged into the output stream, and conversion specifications. Each conversion specification has the following format: introductory % character (optional) one or more flags that modify the behavior of the conversion: -: the result of the conversion is left-justified within the field (by default it is right-justified) +: the sign of signed conversions is always prepended to the result of the conversion (by default the result is preceded by minus only when it is negative) space: if the result of a signed conversion does not start with a sign character, or is empty, space is prepended to the result. It is ignored if + flag is present. # : alternative form of the conversion is performed. See the table below for exact effects otherwise the behavior is undefined. 0 : for integer and floating point number conversions, leading zeros are used to pad the field instead of space characters. For integer numbers it is ignored if the precision is explicitly specified. For other conversions using this flag results in undefined behavior. It is ignored if - flag is present. (optional) integer value or * that specifies minimum field width. The result is padded with space characters (by default), if required, on the left when right-justified, or on the right if left-justified. In the case when * is used, the width is specified by an additional argument of type int. If the value of the argument is negative, it results with the - flag specified and positive field width. (Note: This is the minimum width: The value is never truncated.) (optional) . followed by integer number or *, or neither that specifies precision of the conversion. In the case when * is used, the precision is specified by an additional argument of type int. If the value of this argument is negative, it is ignored. If neither a number nor * is used, the precision is taken as zero. See the table below for exact effects of precision. (optional) length modifier that specifies the size of the argument conversion format specifier The following format specifiers are available: 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 The floating point conversion functions convert infinity to inf or infinity. Which one is used is implementation defined. Not-a-number is converted to nan or nan(char_sequence). Which one is used is implementation defined. The conversions F, E, G, A output INF, INFINITY, NAN instead. Even though %c expects int argument, it is safe to pass a char because of the integer promotion that takes place when a variadic function is called. The correct conversion specifications for the fixed-width character types (int8_t, etc) are defined in the header <inttypes.h> (although PRIdMAX, PRIuMAX, etc is synonymous with %jd, %ju, etc). The memory-writing conversion specifier %n is a common target of security exploits where format strings depend on user input and is not supported by the bounds-checked printf_s family of functions. There is a sequence point after the action of each conversion specifier; this permits storing multiple %n results in the same variable or, as an edge case, printing a string modified by an earlier %n within the same call. If a conversion specification is invalid, the behavior is undefined. | 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). | 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 | 將此調(diào)用到目前為止寫入的字符數(shù)返回給該函數(shù)。結(jié)果寫入?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ù) |
介紹%
人物
(可選)一個或多個修改轉(zhuǎn)換行為的標志:
-
:轉(zhuǎn)換的結(jié)果在字段內(nèi)左對齊(默認情況下它是右對齊的)
+
:帶符號轉(zhuǎn)換的符號總是預設為轉(zhuǎn)換結(jié)果的前綴(默認情況下結(jié)果前面為減號,僅當它為負值時)
空格:如果簽名轉(zhuǎn)換的結(jié)果不是以符號字符開頭,或者是空的,則空格會預設為結(jié)果。如果+
存在標志,則忽略它。
#
:執(zhí)行轉(zhuǎn)換的替代形式。請參閱下表以了解確切的效果,否則行為未定義。
0
:對于整數(shù)和浮點數(shù)轉(zhuǎn)換,前導零用于填充字段而不是空格字符。對于整數(shù),如果明確指定了精度,它將被忽略。對于使用此標志的其他轉(zhuǎn)換會導致未定義的行為。如果-
存在標志,則忽略它。
(可選)整數(shù)值或*
指定最小字段寬度。如果需要,結(jié)果會填充空格字符(默認情況下),右側(cè)對齊時填充空白字符,左側(cè)填充右側(cè)填充。在使用的情況下*
,寬度由類型的附加參數(shù)指定int
。如果參數(shù)的值是負數(shù),則結(jié)果是-
指定的標志和正的字段寬度。(注意:這是最小寬度:該值從不被截斷。)
(可選).
后面跟隨整數(shù)或者*
或者既不指定轉(zhuǎn)換的精度。在使用的情況下*
,精度由類型的附加參數(shù)指定int
。如果這個參數(shù)的值是負數(shù),它將被忽略。如果既不使用數(shù)字也不*
使用,則精度取為零。請參閱下表以了解精確度的確切影響。
(可選)長度修飾符,用于指定參數(shù)的大小
轉(zhuǎn)換格式說明符
以下格式說明符可用:
Conversion
說明符說明參數(shù)類型長度修飾符 hh
(C99)。
h
(none) l
ll
(C99).
j
(C99).
z
(C99).
t
(C99).
L
%
寫文字%
。完整的轉(zhuǎn)換規(guī)范必須是%%
。N / AN / AN / AN / AN / AN / AN / AN / AN / A c
寫入單個字符。該論點首先轉(zhuǎn)換為unsigned char
。如果使用了l修飾符,則首先將參數(shù)轉(zhuǎn)換為字符串,就像通過具有參數(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
,它將轉(zhuǎn)換為char數(shù)組,就像通過調(diào)用wcrtomb
具有零初始化轉(zhuǎn)換狀態(tài)一樣。
N/A N/A char*
wchar_t*
N/A N/A N/A N/A N/A d
i
將有符號的整數(shù)轉(zhuǎn)換為十進制表示形式-dddd。 精度指定出現(xiàn)的最小位數(shù)。默認的精度是1
。
如果轉(zhuǎn)換后的值和精度都是0
沒有字符的轉(zhuǎn)換結(jié)果。
signed char
short
int
long
long long
intmax_t
signed size_t
ptrdiff_t
N / A o
將無符號整數(shù)轉(zhuǎn)換為八進制表示oooo。 精度指定出現(xiàn)的最小位數(shù)。默認的精度是1
。如果轉(zhuǎn)換后的值和精度都是0
沒有字符的轉(zhuǎn)換結(jié)果。在替代實現(xiàn)中,如果需要,可以增加精度以寫入一個前導零。在這種情況下,如果轉(zhuǎ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ù)轉(zhuǎn)換為十六進制表示hhhh。使用x
轉(zhuǎn)換字母abcdef
。
使用X
轉(zhuǎn)換字母ABCDEF
。
精度指定出現(xiàn)的最小位數(shù)。默認的精度是1
。如果轉(zhuǎn)換后的值和精度都是0
沒有字符的轉(zhuǎn)換結(jié)果。在替代實現(xiàn)中, 0x
或者0X
如果轉(zhuǎn)換后的值不為零,則將其作為結(jié)果的前綴。
N / A u
將無符號整數(shù)轉(zhuǎn)換為十進制表示形式dddd。 精度指定出現(xiàn)的最小位數(shù)。默認的精度是1
。如果轉(zhuǎn)換后的值和精度都是0
沒有字符的轉(zhuǎn)換結(jié)果。
N/A f
F
將浮點數(shù)轉(zhuǎn)換為樣式-ddd.ddd中的小數(shù)表示法。 精度指定小數(shù)點后面出現(xiàn)的最小位數(shù)。默認的精度是6
。在替代實現(xiàn)中,即使沒有數(shù)字跟隨,小數(shù)點字符也會被寫入。對于無窮大和非數(shù)字轉(zhuǎn)換風格,請參閱注釋。
N/A N/A double
`double` (C99)
N/A N/A N/A N/A long double
`e`
E
將浮點數(shù)轉(zhuǎn)換為十進制指數(shù)符號。對于e
轉(zhuǎn)換樣式,使用-d.ddd e
±dd。
對于E
轉(zhuǎn)換樣式,使用-d.ddd E
±dd。
指數(shù)至少包含兩位數(shù)字,只有在必要時才使用更多數(shù)字。如果值是0
,指數(shù)也是0
。精度指定小數(shù)點后面出現(xiàn)的最小位數(shù)。默認的精度是6
。在替代實現(xiàn)中,即使沒有數(shù)字跟隨,小數(shù)點字符也會被寫入。對于無窮大和非數(shù)字轉(zhuǎn)換風格,請參閱注釋。
N/A N/A N/A N/A N/A N/A a
A
(C99).
將浮點數(shù)轉(zhuǎn)換為十六進制指數(shù)表示法。對于a
轉(zhuǎn)換樣式- 使用0x
h.hhh p
±d。
對于A
轉(zhuǎn)換樣式- 使用0X
h.hhh P
±d。
0
如果參數(shù)不是標準化的浮點值,則第一個十六進制數(shù)字是。如果值是0
,指數(shù)也是0
。精度指定小數(shù)點后面出現(xiàn)的最小位數(shù)。默認精度足以精確表示值。在替代實現(xiàn)中,即使沒有數(shù)字跟隨,小數(shù)點字符也會被寫入。對于無窮大和非數(shù)字轉(zhuǎn)換風格,請參閱注釋。
N/A N/A N/A N/A N/A N/A g
G
根據(jù)值和精度 將浮點數(shù)轉(zhuǎn)換為十進制或十進制指數(shù)符號。對于風格轉(zhuǎn)換的轉(zhuǎn)換與風格或?qū)⒈粓?zhí)行。gef
對于G
風格轉(zhuǎn)換的轉(zhuǎn)換與風格E
或F
將被執(zhí)行。
讓P
等于精度如果非零,6
如果沒有指定精度,或者1
如果精度是0
。然后,如果具有樣式的轉(zhuǎn)換E
將具有以下指數(shù)X
:
如果P> X≥-4,轉(zhuǎn)換是用式f
或F
和精度P - 1 - X。
否則,轉(zhuǎn)換采用樣式e
或E
精度P - 1。
除非請求替代表示,否則尾隨零將被刪除,如果沒有剩余小數(shù)部分,小數(shù)點字符也會被刪除。對于無窮大和非數(shù)字轉(zhuǎn)換風格,請參閱注釋。
不適用/不適用/不適用/不適用/ n
返回此函數(shù)迄今為止寫入的字符數(shù)。結(jié)果寫入參數(shù)指向的值。規(guī)范可能不包含任何標志,字段寬度或精度。
signed char*
short*
int*
long*
long long*
intmax_t*
簽 size_t*
ptrdiff_t*
N / A p
寫入一個實現(xiàn)定義的字符序列來定義一個指針。不適用不適用不適用不適用不適用不適用 void*
浮點轉(zhuǎn)換函數(shù)將無窮大轉(zhuǎn)換為inf
或infinity
。使用哪一個是實現(xiàn)定義的。
非數(shù)字轉(zhuǎn)換為nan
或。使用哪一個是實現(xiàn)定義的。nan(char_sequence)
該轉(zhuǎn)換F
,E
,G
,A
輸出INF
,INFINITY
,NAN
來代替。
即使%c
需要int
參數(shù),通過char
調(diào)用可變參數(shù)函數(shù)時發(fā)生的整數(shù)提升也是安全的。
對于固定寬度的字符類型(正確的轉(zhuǎn)換規(guī)格int8_t
<inttypes.h>還(雖然,等等)都在頭定義PRIdMAX
,PRIuMAX
等是同義詞%jd
,%ju
等)。
內(nèi)存寫入轉(zhuǎn)換說明符%n
是安全漏洞的常見目標,其中格式字符串取決于用戶輸入,并且不受邊界檢查printf_s
函數(shù)族的支持。
每個轉(zhuǎn)換說明符的操作之后都有一個序列點; 這允許將多個%n
結(jié)果存儲在相同的變量中,或者作為邊緣情況,%n
在同一個調(diào)用中打印由較早修改的字符串。
如果轉(zhuǎn)換規(guī)范無效,則行為未定義。
... - arguments specifying data to print
1,2)發(fā)送到輸出流的字符數(shù)或負值(如果發(fā)生輸出錯誤或編碼錯誤(用于字符串和字符轉(zhuǎn)換說明符))
3)寫入的字符數(shù)buffer
(不包括終止空字符);如果發(fā)生編碼錯誤(用于字符串和字符轉(zhuǎn)換說明符),則返回負值
4)buffer
如果bufsz
被忽略,將被寫入的字符數(shù)(不包括終止空字符),或者如果編碼錯誤(對于字符串和字符轉(zhuǎn)換說明符)發(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
設置上的錯誤。它還指定了額外的轉(zhuǎn)換規(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ù)