Printf output format

Source: Internet
Author: User

Printf output format

Printf output format
The common format of the format string in C is: % [flag] [minimum output width] [. Precision] [length] type. The items in square brackets [] are optional. The meanings of each item are described as follows:
1. Type type characters are used to indicate the type of output data. The format characters and meanings are shown in the following table:
Meaning of Characters in the output format
A floating point number, hexadecimal number, and p-counting (C99)
A floating point number, hexadecimal number, and p-counting (C99)
C Outputs a single character
D. Output signed integers in decimal form (positive numbers do not output symbols)
E outputs Single and Double Precision Real Numbers in exponential form
E outputs Single and Double Precision Real Numbers in exponential form
F outputs Single and Double Precision Real Numbers in decimal form
G is used to output single and double precision real numbers with a shorter output width in % f % e. The % e formula is used when the exponent is smaller than-4 or greater than or equal to the precision.
G uses the shorter output width of % f % e to output a single and double-precision real number. The % e formula is used when the exponent is smaller than-4 or greater than or equal to the precision.
I signed decimal INTEGER (same as % d)
O outputs unsigned integers in octal form (no prefix O is output)
P pointer
S output string
X outputs an unsigned integer in hexadecimal format (the OX prefix is not output)
X outputs an unsigned integer in hexadecimal format (the OX prefix is not output)
U outputs an unsigned integer in decimal format
2. Flag
The following table lists the characters-, +, #, space, and 0:
Logo format character logo meaning
-The result is left aligned with a space on the right.
+ Output symbol (positive or negative)
If the output value of a space parameter is positive, it is preceded by a space.
# It has no effect on Class c, s, d, and u. For Class o, add a prefix of 0 to the output. For Class x,
Add the prefix 0x or 0X to the output; Use the g and G classes to prevent the trailing 0 from being deleted;
For all floating point formats, # ensures that a decimal point character is printed even if it is not followed by any number.
0 fill the field width with leading 0 for All numeric formats. If a-sign or an integer is specified, ignore

3. Minimum output width
The minimum number of digits of the output is expressed by a decimal integer. If the actual number of digits is greater than the defined width, the actual number of digits is output. If the actual number of digits is less than the defined width, spaces or 0 are supplemented.

For example:
Printf ("%-10e \ n", 223.11 );
Printf ("% + 10e \ n", 232.11111111 );

4. Precision
The precision format character starts with "." and is followed by a decimal integer. The meaning of this item is: if a number is output, it indicates the number of digits in decimal places. If the output is a character, it indicates the number of output characters. If the actual number of digits is greater than the defined precision, the part that exceeds the limit is truncated.

5. Length
The length format is h and l. h indicates output by short integer, and l indicates output by long integer.

6. Special usage

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.


There is also a special output format % n which can assign the length value of the output string to a variable, as shown in the following example:
Int slen;
Printf ("111% n", & slen );
After execution, the variable is assigned 3 values.

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.