A brief analysis of the character string format display in C language words

  • 2020-04-02 01:16:23
  • OfStack

symbol                                   role
─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
      % d                           Decimal signed integer
      % I                           The input integer can be an octal number with a leading 0 or a hexadecimal number of 0x
      The % u                           Decimal unsigned integer
      % f                           Floating point Numbers
      % s                           string
      % c                           A single character
      % p                           Pointer value (memory address)
      % e                           A floating point number in exponential form
      % % x, x                   An unsigned integer in hexadecimal notation
      % 0                           An unsigned integer in octal notation
      % % g, g                   Automatically select the appropriate notation
━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━ ━

The general form of the format string is:
[flag][output minimum width][. Precision][length] type
The items in the square brackets [] are optional. The meaning of each item is described as follows:
1. Type type character is used to represent the type of output data, and its format and meaning are shown in the following table:

              Represents the format character format character meaning of the output type

              D outputs signed integers in decimal form (positive Numbers do not print symbols)

              O outputs an unsigned integer in octal form (no prefix o)

              X outputs an unsigned integer in hexadecimal (no prefix OX)

              U outputs an unsigned integer in decimal form

              F outputs single - and double-precision real Numbers as decimals

              E outputs single and double precision real Numbers in exponential form

              G outputs single and double real Numbers with the shorter output width in %f%e

              C outputs a single character

              S output string

2. Mark
The four mark characters are -, +, # and space, the meaning of which is shown in the following table:
Logo format character logo meaning
- align the results to the left and fill in the blanks to the right
+ output symbol (plus or minus) space output value is positive with a space, is negative with a minus sign
# has no effect on c, s, d, u; For class o, prefix the output. For class x, prefix the output with 0x; For e,g, and f, the decimal point is given only when the result has a decimal

3. Output minimum width
Represents the minimum number of digits of the output as a decimal integer. If the actual number is more than the defined width, the output is the actual number. If the actual number is less than the defined width, a space or 0 is added.

Accuracy of 4.
The precision format character begins with ". "and is followed by a decimal integer. If the output number, represents the number of decimal places; If the output is a character, the number of output characters; If the actual number of digits is greater than the precision defined, the excess is truncated.

Length of 5.
The length format is h, and there are two types of l. H means output by short integer and l means output by long integer

Description:
(1) a number can be inserted between "%" and the letter to represent the maximum field width.
For example, %3d indicates the number of 3-bit integer output, which is less than 3-bit right-aligned.
%9.2f refers to the floating point number with the output field width of 9, in which the decimal place is 2, the integer place is 6, and the decimal point occupies one place, not enough for the right alignment of 9 places.
%8s represents a string that outputs 8 characters, less than 8 characters to the right.
If the length of the string, or the number of integer digits, exceeds the specified field width, the string is printed at its actual length.
However, for floating point Numbers, if the integer part of the digits exceed the specified integer bit width, the actual integer bit output; If the decimal number exceeds the specified decimal width, the output is rounded to the specified width.
In addition, if you want to add some zeros before the output value, you should add a zero before the field width term.
For example: %04d means that when a value less than 4 bits is output, 0 will be prefixed to make its total width 4 bits.
If the output format of a character or integer is expressed as a floating point number, the number after the decimal point represents the maximum width and the number before the decimal point represents the minimum width.
For example: %6.9s means to display a string of length not less than 6 and not greater than 9. Anything after the ninth character is deleted if it is greater than 9.

(2) the lowercase letter l can be added between "%" and the letter, indicating that the output is a long number.
For example, %ld means the output long integer, and %lf means the output double floating point number

(3) you can control the left or right alignment of the output, that is, add a "-" sign between "%" and the letter to indicate that the output is left alignment, otherwise it is right alignment.
For example, %-7d means left alignment of the output 7-bit integer, and %-10s means left alignment of the output 10 characters

(4) you can add a * sign between the format character and % to skip the corresponding input data.
For example: the scanf (" % d % d % d ", & a, & b); When you enter 10, 20, 30, the values of a and b are 10 and 30, respectively.


Related articles: