C++ Reference: Standard C++ Library reference: C Library: cstdio: printf

C++官网参考链接:https://cplusplus.com/reference/cstdio/printf/

函数 

printf
int printf ( const char * format, ... );
将格式化的数据打印到标准输出
将按format指向的C字符串写入标准输出(stdout)。如果format包含格式说明符(以%开头的子序列),则format后面的附加实参将被格式化并插入结果字符串中,取代它们各自的说明符。

形参
format
C字符串,包含要写入标准输出(stdout)的文本。
它可以有选择地包含嵌入的格式说明符,这些说明符将被后续附加实参中指定的值替换并按要求格式化。
格式说明符遵循这个原型: 
%[flags][width][.precision][length]specifier
其中,末尾的specifier字符是最重要的组件,因为它定义了对应实参的类型和解释: 

specifier Output Example
or i Signed decimal integer 392
u Unsigned decimal integer 7235
o Unsigned octal 610
x Unsigned hexadecimal integer 7fa
X Unsigned hexadecimal integer (uppercase) 7FA
f Decimal floating point, lowercase 392.65
F Decimal floating point, uppercase 392.65
e Scientific notation (mantissa/exponent), lowercase 3.9265e+2
E Scientific notation (mantissa/exponent), uppercase 3.9265E+2
g Use the shortest representation: %e or %f 392.65
G Use the shortest representation: %E or %F 392.65
a Hexadecimal floating point, lowercase -0xc.90fep-2
A Hexadecimal floating point, uppercase -0XC.90FEP-2
c Character a
s String of characters sample
p Pointer address b8000000
n

Nothing printed.
The corresponding argument must be a pointer to a signed int.
The number of characters written so far is stored in the pointed location.

(不打印出来。

对应的实参必须是指向signed int的指针。

到目前为止写入的字符数存储在指向的位置。)

% A % followed by another % character will write a single % to the stream. %

格式说明符还可以包含子说明符:flags、width、.precision和modifiers(按此顺序),它们是可选的,并遵循以下规范:

flags description
- Left-justify within the given field width; Right justification is the default (see width sub-specifier).(在给定的字段宽度内左对齐;右对齐是默认的(参见width子说明符)。)
+

Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign.

(甚至对于正数,强制在结果前面加上正号或负号(+或-)。默认情况下,只有负数前面有-号。)

(space)

If no sign is going to be written, a blank space is inserted before the value.

(如果不写入符号,则在值之前插入一个空白格。)

#

Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero.
Used with a, A, e, E, f, F, g or G it forces the written output to contain a decimal point even if no more digits follow. By default, if no digits follow, no decimal point is written.

(与o、x或X说明符一起使用时,对于不等于0的值,该值的前面分别加0、0x或0X。

与a,A,e,E,f,F,g或G一起使用,它强制书面输出包含小数点,即使后面没有其他数字。默认情况下,如果后面没有数字,则不写小数点。)

0

Left-pads the number with zeroes (0) instead of spaces when padding is specified (see width sub-specifier).

(当指定填充时,用零(0)代替空格左填充数字(参见width子说明符)。)

width description
(number)

Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.

(要打印的最小字符数。如果要打印的值短于此数字,结果将用空白格填充。即使结果更大,该值也不会被截断。)

*

The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

(width不是在format字符串中指定的,而是作为必须格式化的实参之前的一个附加整数值实参。)

.precision description
.number

For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0.
For a, A, e, E, f and F specifiers: this is the number of digits to be printed after the decimal point (by default, this is 6).
For g and G specifiers: This is the maximum number of significant digits to be printed.
For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.
If the period is specified without an explicit value for precision, 0 is assumed.

(对于整数说明符(d, i, o, u, x, X): precision指定要写入的最小位数。如果要写入的值短于此数字,则用前导0填充结果。即使结果较长,该值也不会被截断。precision为0意味着没有为值0写入字符。

对于a, A, e, E, f和F说明符:这是小数点后要打印的位数(默认情况下,这是6)。

对于g和G说明符:这是要打印的有效数字的最大数量。

对于s:这是要打印的最大字符数。默认情况下,打印所有字符,直到遇到结束的空字符。

如果指定的周期没有明确的precision值,则假定为0。)

.*

The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

(precision不是在format字符串中指定的,而是作为必须格式化的实参前面的附加整数值实参指定的。)

length子说明符修改数据类型的长度。这是一个图表,显示了用于解释有或没有length说明符的对应实参的类型(如果使用了不同的类型,在允许的情况下,执行适当的类型提升或转换):

specifiers
length d i u o x X f F e E g G a A c s p n
(none) int unsigned int double int char* void* int*
hh signed char unsigned char signed char*
h short int unsigned short int short int*
l long int unsigned long int wint_t wchar_t* long int*
ll long long int unsigned long long int long long int*
j intmax_t uintmax_t intmax_t*
z size_t size_t size_t*
t ptrdiff_t ptrdiff_t ptrdiff_t*
L long double

注意,c说明符接受int(或wint_t)作为实参,但在格式化输出之前执行到char值(或wchar_t)的适当转换。
注:黄色行表示C99引入的说明符和子说明符。有关扩展类型的说明符,请参见
...(附加实参) 
根据format字符串的不同,函数可能需要一系列附加实参,每个实参包含一个值,用于替换format字符串中的格式说明符(对于n是指向存储位置的指针)。
这些实参的数量至少应该与格式说明符中指定的值的数量相同。函数将忽略额外的实参。

返回值
如果成功,则返回写入的字符总数。
如果发生写错误,则设置错误指示符(ferror)并返回负数。
如果在写入宽字符时发生多字节字符编码错误,则errno设置为EILSEQ并返回负数。

用例
/* printf example */
#include

int main()
{
   printf ("Characters: %c %c \n", 'a', 65);
   printf ("Decimals: %d %ld\n", 1977, 650000L);
   printf ("Preceding with blanks: %10d \n", 1977);
   printf ("Preceding with zeros: %010d \n", 1977);
   printf ("Some different radices: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100);
   printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);
   printf ("Width trick: %*d \n", 5, 10);
   printf ("%s \n", "A string");
   return 0;

输出:

C++ Reference: Standard C++ Library reference: C Library: cstdio: printf_第1张图片

兼容性
特定的库实现可能支持额外的说明符和子说明符。
这里列出的是由最新的C和C++标准支持的(都是在2011年发布的),但是黄色的那些是在C99中引入的(只有C++11之后的C++实现才需要),并且可能不被遵循旧标准的库所支持。 

另请参考
puts    Write string to stdout (function) (写字符串到标准输出(函数))
scanf    Read formatted data from stdin (function) (从标准输入读格式化数据(函数))
fprintf    Write formatted data to stream (function) (写格式化数据到流(函数))
fwrite    Write block of data to stream (function) (写数据块到流(函数))

你可能感兴趣的:(C++,Reference,C,Library,c++,c语言,printf,打印格式化数据到标准输出)