Detailed description of C language printf()

function

printf //header file intclude "stdio.h"

int printf ( const char * format, ... );

describe

Print the formatted data to standard output, writing the C string pointed to by format to standard output (stdout). If format contains format specifiers (subsequences beginning with %), additional arguments following format will be formatted and inserted into the resulting string, replacing their respective specifiers.

parameter

format

C string containing the text to be written to stdout. It can optionally contain embedded format specifiers, which are replaced by values ​​specified in subsequent additional parameters, and formatted as requested.
Format specifiers follow this prototype: [see compatibility notes below]

%[flags][width][.precision][length]specifier
%[标志][宽度][.精度][长度]说明符,其中末尾的说明符

where, at the end ofspecifier character[specifier] is the most important component, as it defines the type and the interpretation of the corresponding parameters:

Specifier / specifier Output Example
d 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 number, lower case 392.65
F base floating point number, uppercase 392.65
e scientific notation (mantissa/exponent), lower case 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 number, lower case -0xc.90fep-2
A Hexadecimal floating point number, uppercase -0XC.90FEP-2
c character a
s string sample
p pointer address b8000000
n Nothing is printed. The corresponding argument must be a pointer to a signed int. The number of characters written so far is stored at the location pointed to.
% A % followed by another % character will write a single % to the stream %

Format specifiers can also contain sub-specifiers: flags, width, .precision, and modifiers [flags, width, .precision and modifiers] (in that order), they are optional and follow the following conventions:

flags/flags describe
- Left-aligned within the given field width; right-aligned is the default (see width sub-specifiers).
+ Even in positive numbers, a plus or minus sign (+ or -) is prefixed to the result. By default, only negative numbers are preceded by a - sign.
(space) If no symbols are intended to be written, a space is inserted before the value.
# When used with the o, x, or X specifiers, for values ​​less than zero, the value is preceded by 0, 0x, or 0X, respectively. When used with a, A, e, E, f, F, g, or G, it forces the written output to include a decimal point even if no more digits follow. By default, no decimal point is written if no digits follow it.
0 When padding is specified, numbers are left-padd with zeros (0) instead of spaces (see width sub-specifier).
width/width describe
(number) The minimum number of characters to print. If the value to be printed is less than this number, the result is padded with spaces. Even if the result is larger, the value is not truncated.
* The width is not specified in the format string, but as an additional integer-valued parameter preceding the parameters that must be formatted.
.precision/.precision describe
.number For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to write. If the value to be written is shorter than this number, the result will be padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no characters are written for a value of 0. For the a, a, e, e, f and F specifiers: this is the number of digits to print after the decimal point (6 by default). For the g and G specifiers: this is the maximum number of significant digits to print. For s: This is the maximum number of characters to print. By default, all characters will be printed until a trailing null character is encountered. If no explicit precision value is specified when a period is specified, 0 is assumed.
.* The precision is not specified in the format string, but as an additional integer-valued parameter preceding the parameters that must be formatted.

The length sub-specifier modifies the length of the data type. Here's a diagram showing the types used to interpret the corresponding arguments with and without length specifiers (and performing the correct type promotion or conversion (if allowed) if a different type is used):

specifier
Length/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

关于说明符的注意事项:它接受 int(或 wint_t)作为参数,但在格式化输出之前执行对 char 值(或 wchar_t)的正确转换。

... (additional arguments)

根据格式字符串,函数可能需要一系列附加参数,每个参数都包含一个值,用于替换格式字符串中的格式说明符(或指向 n 的存储位置的指针)。
这些参数的数量至少应与格式说明符中指定的值数一样多。函数会忽略其他参数。

返回值

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

/* printf example */
#include <stdio.h>
int main()
{
    
    
   printf ("Negative number: %-d\n", -10);
   printf ("Characters: %c %c \n", 'a', 18);
   printf ("Decimals: %d %ld\n", 18, 650000L);
   printf ("Preceding with blanks: %10d \n", 18);
   printf ("Preceding with zeros: %010d \n", 18);
   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;
}

输出:

Negative number: -10
Characters: a
Decimals: 18 650000
Preceding with blanks:         18 
Preceding with zeros: 0000000018 
Some different radices: 100 64 144 0x64 0144 
floats: 3.14 +3e+00 3.141600E+00 
Width trick:    10 
A string 

Guess you like

Origin blog.csdn.net/qq_42815643/article/details/129118943