Transferred from: http://www.cnblogs.com/scbzljstudy/archive/2011/02/28/1966887.html
1 General format
printf (format control, output table column) For example: printf ("i=%d,ch=%c\n", i,ch); Description: (1) "Format Control" is a string enclosed in double apostrophes, also known as a "conversion control string," which includes two kinds of information: ① format description: Consists of "%" and format characters, which is the function of converting the output data to the specified format output. ② ordinary characters, which are characters that need to be output as-is. (2) "Output table column" is some data that needs to be output, can be an expression (3) The general form of the printf function can be expressed as printf (parameter 1, Parameter 2, ..., parameter n) function is to output the parameter 2~ parameter N in the format given by parameter 1
2 Format characters (9 types) (1) d (or i) format character. Used to output decimal integers, there are several uses: ①%d, output by the actual length of the integer data. ②%md,m the width of the specified output field. If the number of bits in the data is less than M, then the left is a space, and if it is greater than M, the actual number of digits is output. ③%ld (%mld also), output long integer data. For example: Long a=123456; printf ("%ld", a); (2) o format character, which outputs integers as octal numbers. Format:%o,%mo,%lo,%mlo is available. (3) x (or x) format character, which outputs integers as hexadecimal digits. Format:%X,%MX,%LX,%MLX is available. (4) u format character, used to output unsigned type data, that is, unsigned number, in the form of a decimal number output. Format:%u,%mu,%lu is available. See:li4-3.c (5) C format character, which is used to output a character. Format:%C,%MC is available. (6) s format character, used to output a string. Format:%S,%MS,%-MS,%M.NS,%-M.NS is available. (7) F format character, which is used to output real numbers (including single and double precision) and output in decimal form. Format:%F,%M.NF,%-M.NF is available. Note: The number of significant digits for single-precision real numbers is typically 7Bits, with a double precision of 16 bits. (8) e (or e) format character, which outputs real numbers in exponential form. Format:%e,%m.ne,%-m.ne is available. (9) the G (or G) format character, which is used to output a real number, is automatically selected in f format or e format (one of the smaller widths when the output is selected), depending on the size of the value.
3 Description(1) In addition to X, E, G (denoted in uppercase letters), other format characters must be in lowercase letters, (2) "format control" string can contain escape characters, (3) If you want to output the character "%", you should be in the "format control" string with two consecutive%, such as: printf ("%f%%", 1.0/3); (4) Format character table refer to the following table 1 printf format characters
format character |
Say & nbsp; |
d,i |
|
o |
|
x,x |
|
u |
|
c |
Enter as character Out, only one character is output |
s |
output string |
f< /td> |
output single-, double-precision numbers in decimal form, implicitly output 6-bit decimal |
e,e |
output real in exponential form Number |
g,g |
Choose a format with a shorter output width in%f or%e format, not output meaningless 0 |
Table 2 additional format description characters for printf
C Language Output Format summary