Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Who can help me explain this C language topic? I don't understand.
Who can help me explain this C language topic? I don't understand.
1. conversion specifier

%a(%A) floating-point number, hexadecimal number and p-(P-) notation (C99)

%c character

%d signed decimal integers

%f floating-point numbers (including float and doulbe)

%e(%E) floating-point exponent output [e-(E-) notation]

%g(%G) floating-point numbers do not display meaningless zero "0".

%i signed decimal integer (same as %d)

%u unsigned decimal integers

%o octal integer, such as 0 123.

%x(%X) hexadecimal integer 0f(0F), such as 0x 1234.

%p pointer

%s string

%% "%"

mark

Left alignment: "-"For example, "%-20s"

Right alignment: "+"For example: "%+20s"

Spaces: If the symbol is positive, spaces will be displayed; If it is negative, a "-"sign will be displayed, for example, "%6.2f".

#: It has no influence on classes C, S, D and U; For class o, prefix o when outputting; For class X, the prefix 0x is output;

For classes E, G, F, G and F, the decimal point is given only when the result has a decimal point.

3. Format string (format)

[Mark] [Output Minimum Width] [. Accuracy] [Length] type

"%-MD": Left-aligned. If m is less than the actual value, it is output according to the actual value.

"%m.ns": output m bits, take n bits of the string (from the left), and fill in the space on the left. When n >; When m or m is omitted, m = n.

For example, "%7.2s" is entered into China.

Output "CH"

"%m.nf": outputs floating-point numbers, where m is the width and n is the number to the right of the decimal point.

For example, "%3. 1f" enters 3852.99.

Output 3853.0

Length: h short molding quantity, l long molding quantity.

Full format of printf format control:

%-0 m.n l or h format characters

The following is a description of the items that make up the format description:

①%: the opening symbol of format description, which is essential.

②-:Yes-indicates left-aligned output; If omitted, it means right-aligned output.

③0: 0 means that the specified space is filled with 0, and if omitted, it means that the specified space is not filled.

④ m.n: m refers to the domain width, that is, the number of characters occupied by the corresponding output item on the output device. N refers to accuracy. The number of decimal places used to describe the output real number. When n is specified for, the implied precision is n=6 bits.

⑤l or h:l means long type of integer and double type of real type. H is used to change the format character of an integer to a short integer.

-

layout/format character

Format characters are used to specify the data type and output format of the output item.

①d format: used to output decimal integers. There are the following usages:

%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 data is less than m, the left end is filled with spaces; If it is greater than m, it is output according to the actual number of digits.

%ld: output long integer data.

②o format: output integers in unsigned octal format. Long integers can be output in "%lo" format. You can also specify the width of the field output in "%mo" format.

Example:

Master ()

{ int a =- 1;

printf("%d,%o ",a,a);

}

Operation results:-1, 177777

Program analysis:-1 (stored in the form of complement) in the memory unit is (111111/.

③x format: output integers in unsigned hexadecimal form. Long integers can be output in "%lx" format. You can also specify the field width and output it in "%mx" format.

④u format: output integers in unsigned decimal form. Long integers can be output in "%lu" format. You can also specify the field width to output in "%mu" format.

⑤c format: output a character.

⑥s format: used to output a string. There are several uses.

%s: For example, printf("%s ","CHINA ") outputs the" CHINA "string (excluding double quotation marks).

%ms: The output string occupies m columns. If the length of the string itself is greater than m, it will break through the limit of obtaining m and output all strings. If the string length is less than m, the left blank is filled.

%-ms: If the length of the string is less than m, the string will be left and right filled with spaces within the range of m columns.

%m.ns: The output occupies m columns, but only the left n characters in the string are taken. These n characters are output on the right side of column M, with spaces on the left.

%-m.ns: where m and n have the same meaning, n characters are output on the left side of the range of m columns, and spaces are filled on the right side. If n>m is used, the value of n is automatically taken, which ensures the normal output of n characters.

⑦f format: used to output real numbers (including single precision and double precision) in decimal form. There are the following usages:

%f: No width is specified, all integer parts are output, and 6 decimal places are output.

%m.nf: the output * * * occupies m columns, including n decimal places, such as the left blank with a value width less than m.

%-m.nf: the output * * * occupies n columns, including n decimal places, for example, the width of the value is less than the right end of m to fill in spaces.

⑧e format: output real numbers in exponential form. The following forms can be used:

%e: The number part (also called mantissa) outputs 6 decimal places, and the exponent part occupies 5 or 4 digits.

The characters %m.ne and %-m.ne: m, n and "-"have the same meanings as before. Where n refers to the number of decimal places in the digital part of the data, and m refers to the width occupied by the whole output data.

⑨g format: automatically select the shorter output of F format or E format, and do not output meaningless zeros.

-

Further explanation of printf function;

If you want to output the character "%",you should use two consecutive% in the "format control" string, such as:

printf("%f%% ", 1.0/3);

The output is 0.333333%.

-

For single-precision numbers, only the first 7 digits are valid, and the decimal number is 6 digits.

For double-precision numbers, the first 16 bits are significant and have 6 decimal places when they are output with %lf format symbols.

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

Under the guidance of a master

The format of m.n can also be expressed in the following way (for example).

char ch[20];

printf("%*。 *s\n ",m,n,ch);

The front * defines the total width, and the back defines the output quantity. Corresponding to the external parameters m and n respectively. I think the advantage of this method is that you can assign values to parameters m and n outside the statement, thus controlling the output format.

-

Today (06.6.9), I saw another output format% n. You can assign the length value of the output string to a variable, as shown in the following example:

int slen

printf("hello world%n ",& ampslen);

After execution, the variable is assigned to 1 1.

This article is from CSDN blog, please indicate the source:/yuntongsf/archive/2009/08/29/4497335.aspx.