C output format Summary

Source: Internet
Author: User

 

1 General Format

Printf (format control, output table column) for example: printf ("I = % d, CH = % C/N", I, CH );

Note:

(1) "format control" is a string enclosed by a dual-marker, also known as "Conversion Control string". It contains two types of information:

① Format description: it consists of "%" and format characters. It is used to convert the output data to the specified format output.

② Common character, that is, the character that needs to be output as is.

(2) "output table column" is the data to be output, which can be an expression.

(3) The general form of the printf function can be expressed

Printf (parameter 1, parameter 2 ,......, Parameter n)

The function is to set parameters 2 ~ Parameter n is output in the format specified by parameter 1.

2 format characters (9 types)

(1) D (or I) format. It is used to output a decimal integer. It can be used in the following ways:

① % 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 (% MLD), which outputs long integer data.

Example: Long A = 123456;

Printf ("% lD", );

(2) o Format operator, which outputs integers in the form of Octal numbers. Format: % O, % Mo, % Lo, % MLO.

(3) x (or X) format operator, which outputs integers in hexadecimal format. Format: % x, % MX, % lx, % mlx.

(4) U format, used to output unsigned data, that is, the number of unsigned data, in decimal format. Format: % u, % Mu, % lu.

(5) The C format character, used to output a character. Format: % C, % MC.

(6) s format character, used to output a string. Format: % s, % ms, %-MS, % m. NS, %-M. ns.

(7) F format character, used to output real numbers (including Single and Double Precision), in decimal form. Format: % F, % m. NF, %-M. NF.

Note: The valid digits of Single-precision real numbers are generally 7 bits and the double precision is 16 bits.

(8) E (or E) format operator, which outputs real numbers in exponential form. Format: % E, % m. Ne, %-M. ne.

(9) g (or g) format character, used to output real numbers, according to the value size, automatically select the F format or eformat (select a type with smaller output width ).

3 Description

(1) Except for X, E, and g (in uppercase letters), other characters must be in lowercase;

(2) The "format control" string can contain escape characters;

(3) If you want to output the character "%", you should use two consecutive % in the "format control" string, for example:

Printf ("% F %", 1.0/3 );

(4) For details about the format, see the following table.

Table 1 printf format characters

Format characters

Description

D, I

Outputs integers in a signed decimal form (positive numbers do not output symbols)

O

Returns an integer in octal notation (0 is not the leading character)

X, X

Returns an integer in the hexadecimal notation (0x is not output). X is used to output A ~ of the hexadecimal notation ~ F is output in lowercase, and X is output in uppercase letters.

U

Returns an integer in unsigned decimal format.

C

Output in character form, only one character

S

Output string

F

Outputs Single and Double Precision in decimal form, and outputs 6 decimal places implicitly.

E, E

Output real numbers in exponential form

G, G

Select a format with a shorter output width in % f or % eformat, and do not output meaningless 0

Table 2 Additional formats of printf

 

Real column:

Void main ()

{

Int A = 123;
Float B = 1.23f;

Int c = 1;

Printf ("%-4D \ n % 4D \ n % 2D \ n % 1D \ n % d \ n", a, );
Printf ("%-4.3f \ n % 4f \ n % 2f \ n % 1f \ n % F \ n", B, B );
Printf ("% 2D \ n %-2D \ n", C, C );

}

The running result is as follows:
In the form of 123 _ %-MD, the actual field width is smaller than the specified value, so right fill with spaces;
_ 123% MD format. The actual field width is smaller than the specified one, so left spaces are filled;
In the form of 123% MD, the actual field width is greater than the specified value, so the output is based on the actual number of digits;
123 same as above
123 output width not specified, output by actual number of digits
In the form of 1.230%-M. NF, retain N decimal places and specify M-bit fields. Because the actual number of digits is larger than the specified width (see note), output by actual number of digits
In the form of 1.230000% MF, the number of decimal places is not specified. The default value is 6 bits. Because the actual number of BITs is larger than the specified width, the output is based on the actual number of bits.
In the form of 1.230000% MF, the number of decimal places is not specified. The default value is 6 bits. Because the actual number of BITs is larger than the specified width, the output is based on the actual number of bits.
1.230000% F format. The default number of decimal places to be retained is 6 bits. No output field is specified.
_ 1% MD format. The actual field width is smaller than the specified one, so left spaces are filled.

In the form of 1 _ %-MD, the actual field width is smaller than the specified one, so right fill with spaces

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.