The complete format of printf format control:
%-0 M. n l or H characters
The following describes the composition formats:
① %: Indicates the starting symbol of the format description, which is indispensable.
②-: "Yes" indicates the left-aligned output. If it is omitted, "yes" indicates the right-aligned output.
③ 0: if there is 0, it indicates that the specified vacant space is filled with 0. If it is omitted, it indicates that the specified vacant space is not filled.
④ M. N: M indicates the domain width, that is, the number of characters that the corresponding output item occupies on the output device. N indicates the precision. It is used to describe the decimal places of the output real number. If n is specified, the implicit precision is n = 6 digits.
⑤ L or H: L refers to long type for integer type and double type for real type. H is used to modify the integer format characters to the short type.
Format characters
The format character is used to specify the data type and output format of the output item.
① D format: Used to output a decimal integer. It has the following usage:
% D: output according to the actual length of integer data.
% MD: m is the width of the specified output field. If the number of digits of the data is smaller than m, spaces are filled on the left side. If the number is greater than m, the data is output based on the actual number of digits.
% Ld: Output long integer data.
② O Format: outputs integers in the unsigned octal form. You can output long integers in "% Lo" format. You can also specify the field width to output in "% Mo" format.
Example:
Main ()
{Int A =-1;
Printf ("% d, % O", a, );
}
Running result:-1, 177777
Program parsing: the value of-1 is (1111111111111111) 2 in the inner storage unit (stored in the form of a supplemental code), and the value of-1 is (177777) 8.
③ X format: returns an integer in the unsigned hexadecimal format. You can output long integers in "% lx" format. You can also specify the field width to output in "% MX" format.
④ U format: returns an integer in unsigned decimal format. You can output long integers in "% lu" format. You can also specify the field width to output in "% mu" format.
⑤ C format: Output a character.
6 s format: Used to output a string. Usage
% S: for example, printf ("% s", "China") Outputs A "China" string (excluding double quotation marks ).
% MS: The output string occupies the m column. If the length of the string is greater than m, the limit of M is exceeded and all strings are output. If the string length is less than m, spaces are left.
%-MS: if the length of a string is less than m, the string is left-aligned and right-filled with spaces within the m column range.
% M. NS: the output occupies the m column, but only takes n characters from the left of the string. The n characters are output to the right of the m column with spaces left.
%-M. NS: the meaning of M and N is the same as above. n characters are output to the left of the m column range, and spaces are filled on the right. If n> m, the N value is automatically taken, that is, the normal output of n characters is guaranteed.
7. F format: Used to output real numbers (including Single and Double Precision) in decimal form. It has the following usage:
% F: the width is not specified. All integers are output and 6 decimal places are output.
% M. NF: the output occupies M columns, where n decimal places exist. For example, if the value width is less than m, spaces are filled at the left end.
%-M. NF: the output occupies n columns, where n decimal places exist. For example, if the value width is less than m, fill in spaces at the right end.
⑧ Eformat: returns the real number in exponential form. The following formats are available:
% E: The number part (also called the ending number) Outputs 6 decimal places, and the index part occupies 5 or 4 digits.
The characters % m. Ne and %-M. Ne: M, N, and "-" are the same as those before. Here, N refers to the decimal point of the numeric part of the data, and M indicates the width of the entire output data.
⑨ G format: automatically selects a short type of output in the F format or the E format, and does not output meaningless zero.
Further description of the printf function:
If you want to output the character "%", you should use two consecutive % characters in the "format control" string, for example:
Printf ("% F %", 1.0/3 );
0.333333% output.
For the single-precision number, only the first seven digits are valid digits and 6 decimal places when the % F format is used for output.
For the double-precision number, when the % lf format operator is used for output, the first 16 digits are valid digits with 6 decimal digits.
Guidance from experts
The format of M. N can also be expressed as follows (for example)
Char ch [20];
Printf ("% *. * s/n", M, N, CH );
* Defines the total width and the number of outputs. The Parameter M and N correspond to the outer parameters respectively. I think the advantage of this method is that the M and N parameters can be assigned outside the statement to control the output format.
Today (06.6.9), we see another output format: % N. You can assign the length value of the output string to another variable, as shown in the following example:
Int slen;
Printf ("Hello world % N", & slen );
After execution, the variable is assigned a value of 11.
I checked again and saw an article (View) saying that the output in this format has been confirmed as a security risk and has been disabled. Search again to find out what overflow, vulnerabilities, and so on are all used for this usage. You can find one: Format String Attack notes
Pay special attention to the usage of % *. * s.
From: http://blog.liuwentao.net/read.php? 17 character ---------------- escape character reference:/a: beep, bell
/B: Roll Back: one row backward
/F: Form feed
/N: line feed, cursor to the beginning of the downstream line
/R: Press enter and move the cursor to the beginning of the line.
/T: horizontal tabulation
/V: vertical tabulation
//: Backslash
/': Single quotes
/": Double quotation marks
/? : Question mark
/DDD: three octal nodes
/Xhh: two-digit hexadecimal format
/0: Empty character (null), nothing to note:
1,/V vertical tabulation and/F break do not affect the screen, but will affect the printer to perform the response operation.
2,/N should actually call the carriage return to wrap the line. Line feed only changes one line without changing the horizontal coordinates of the cursor. Press enter to return to the beginning of the line without changing the vertical coordinates of the cursor.
3,/t move the cursor forward four or eight cells, which can be set in the Compiler
4,/'is used in characters (that is, in single quotes. It is not required in the string (that is, double quotation marks). You only need to use.
5 ,/? Actually not necessary. As long as it is used? You can (verify in Windows vc6 and TC2 ). For details about the format controller, refer to: output character: printf usage: outputs several types of arbitrary data to the terminal. Format: printf (format controller, output list) Description: Format Controller: % format description guide. -Specify the left-aligned output. 0: Fill in the specified blank space. M. N specifies the output domain width and accuracy. Correction of the output length of L. H. The format character specifies the output data type. Description: Format character: Specifies the data type and output format of the output item. D: A signed decimal integer. O unsigned octal number. X unsigned hexadecimal number. (In lowercase x format, lowercase letters a, B, c, d, e, f are used to represent the numbers between 10 and 15, uppercase X uses uppercase abcdef to represent the number between 10 and 15.) U is an unsigned decimal integer. Basic INTEGER: two bytes, sixteen bits (digits) 11 11 11 11 11 11 11 11 11 11 11 equals 65535.11 11 11 11 11 11 11 11 equals-2. the first (symbol bit) of a negative number is 1. How can I get the complement of a negative number: the original code is reversed by adding one. The binary number of 2 is: 00 00 00 00 00 10. The inverse value is equivalent to 11 11 11 11 11 11 11 01, and then 1. the binary number goes into two places, so we can get the binary code of-2. You can use % d to output signed integer numbers. Unsigned integer numbers can be output using % u. The number stored in the computer is a binary number. The highest bit is a symbol bit or a data bit, which can be controlled by an output format character. Binary Number to octal number: Three 1 corresponds to one 7. Binary Number hexadecimal number: Four Binary numbers correspond to one hexadecimal number, and four 1 corresponds to one F. Description: Format character: c Outputs a character. S outputs a string. E. output the real number in exponential form. F outputs the real number in decimal form. G automatically determines that the output format is short in E and F, and does not print invalid zeros. % Output %. Note: For Single-precision data output using the % F format, only the first seven digits are valid digits and the decimal digits are 6 digits. For the double-precision number, when the % lf format operator is used for output, the first 16 digits are valid digits with 6 decimal digits. Length modifier L: Specify the long integer for the integer type. For example: % lD, % lx, % Lo, % lu, specify the double precision for the real type. For example: % lf h: the format characters used only for integer types are corrected as follows: % HD, % HX, % ho, % Hu for 64-bit integer input and output, in the C ++ environment on poj (VC ++), the 64-bit integer is:
_ Int64
The input/output format is "% i64d". In the G ++ environment, the 64-bit integer is
Long long
The input and output formats are "% LLD ". description of the field width and accuracy M: The field width, that is, the number of characters that the corresponding output item occupies on the output device. If the actual data width is greater than m, the output is based on the actual data width. Actual data width <m, left blank. N: precision, indicating the number of decimal places of the output real number. Note: The Compiling Program only checks the call form of the printf function and does not analyze the format control string. If the format character does not match the type of the output item, no type conversion is performed. (The number of outputs is random.) characters must contain lowercase letters. The format control string can contain escape characters. Normal characters in the format control string are output as is. Parameters of an output item can be expressions and function calls in addition to constants and variables. % G, % F, and % E are used to print floating point values.
The % G format is especially useful for printing floating-point numbers that do not require column-aligned. It has two functions:
1. Remove the excess zero at the end of the number (the number does not reach six digits)
2. Retain the six valid digits (the remaining six digits)
When the % eformat is used to print floating point numbers, it is displayed in the exponential form. For example, when the circumference rate is output, it is 3.141593e + 00.
Differences between the two:
The number printed in % G format is a total of 6 Valid digits.
% Eformat: print the 6-digit valid number after the decimal point
% F do not use an exponential representation of floating point numbers. Therefore, the circumference rate is 3.141593.
(But pay attention to its precision requirement: it is also a 6-digit valid number after the decimal point)
(8) % is used to print a % character.
(9) % E and % G only use uppercase/lowercase letters (e) instead of lowercase letters (e) to output positive and negative numbers. For example:
Printf ("% + D/N",-5, 0, 5 );
You only need to add a "+" in the middle. The role is to output the sign bit (that is, the plus or minus sign of the number)
If you do not want the '+' sign to appear before a positive number, use the following method:
You only need to add a space in the middle. For example:
Function: If a number is non-negative, a space is inserted before it.
Int I;
For (I =-3; I <= 3; I ++)
Printf ("% d/N", I); // note the usage tips of sscanf () with a space between % and D:
To break down strings, many functions require the knowledge of regular expressions, so we will introduce several simple methods to use sscanf to break down strings.
1.
Char STR [2, 100], str1 [100], str2 [100];
Gets (STR );
Sscanf (STR, "% S % s", str1, str2 );
Splits the entire line of string into two strings by space, tab, or carriage return.
2
Returns the string of the specified length. In the following example, a string with a maximum length of 4 bytes is obtained.
Sscanf ("123456", "% 4 s", STR); this article from the csdn blog, reprinted please indicate the source: http://blog.csdn.net/qiqi5521/archive/2008/04/19/2306364.aspx