C format output

Source: Internet
Author: User

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 m 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.

####################################### Collection # #######################################
Expert guidance
The format of m. n can also be expressed as follows (for example)
Charch [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, we can see an output format % n which can assign the length value of the output string to a variable. See the following example:

Intslen;

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.

 

 

 


1. Conversion specifiers
% A (% A) floating point number, hexadecimal number, and p-(P-) Notation (C99)
% C characters
% D signed decimal integer
% F floating point number (including float and doulbe)
% E (% E) floating point number index output [e-(E-) Note]
% G (% G) floating point number does not show meaningless zero "0"
% I signed decimal INTEGER (same as % d)
% U unsigned decimal integer
% O octal integer e.g. 0123
% X (% X) hexadecimal integer 0f (0F) e.g. 0x1234
% P pointer
% S string
% "%"

2. Flag
Left alignment: "-" e.g. "%-20 s"
Right alignment: "+" e.g. "% + 20 s"
Space: If the symbol is positive, space is displayed. If the symbol is negative, "-" e.g. "% 6.2f" is displayed"
#: It has no effect on Class c, s, d, and u. It adds the prefix o to class o in output; it also adds the prefix 0x to Class x in output;
The decimal point is given for Class e, g, and f when the result has decimal places.

3. Format String (Format)
[Flag] [minimum output width] [. Precision] [length] Type
"%-Md": Left alignment. If the m ratio is small, it is output as needed.
"% M. ns": output m-bit. Take the string (left-Start) n-bit and fill the left space. When n> mor m is omitted, m = n
E.g. "% 7.2 s" input CHINA
Output "CH"
"% M. nf": Output floating point number. m indicates the width, and n indicates the right digit of the decimal point.
E.g. "% 3.1f" input 3852.99
3853.0 output
Length: h short integer, l long integer

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.

####################################### Collection # #######################################
Expert guidance
The format of m. n can also be expressed as follows (for example)
Charch [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 ("helloworld % n", & slen );

The variable is assigned a value of 11 after execution.

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.