Printf output format Summary

Source: Internet
Author: User

The printf function is called the format output function. The last letter of the keyword F indicates "format. The function is to display the specified data to the display screen in the format specified by the user.

General Form of printf function call

The printf function is a standard library function. Its function prototype is in the header file "stdio. H. However, as a special case, you do not need to include stdio. H files before using the printf function. The printf function is generally called in the following format:
Printf ("format control string", output table column)
The format control string is used to specify the output format. The format control string can be a format string or a non-format string. The format string is a string starting with "%". It is followed by characters in different formats to indicate the type, format, length, and decimal places of the output data. For example:

  • "% D" indicates output in decimal integer;
  • "% LD" indicates the output is a long integer in decimal format;
  • "% C" indicates output based on bytes.

The unformatted string is output as is, which plays a role in display. Each output item is provided in the output table. The format string and each output item must correspond one to one in quantity and type.

Format String

1) Type
The type character is used to indicate the type of output data. Its format characters and meanings are shown in the following table:

Format characters Meaning
D Outputs signed integers in decimal form (positive numbers do not output symbols)
O Output an unsigned integer in octal format (no prefix 0 is output)
X, X Output an unsigned integer in hexadecimal format (the ox prefix is not output)
U Returns an unsigned integer in decimal format.
F Output Single and Double Precision Real Numbers in decimal form
E, E Output Single and Double Precision Real Numbers in exponential form
G, G Output Single and Double precision real numbers with a shorter output width in % f or % E
C Output Single Character
S Output string
 

2) Flag
The following table lists the four types of Logo characters:-, +, #, and space:

Logo Meaning
- The result is left aligned with a space on the right.
+ Output symbol (positive or negative)
Space When the output value is positive, it is preceded by a space.
# It has no effect on Class C, S, D, and U;
For the o class, add the prefix O to the output;
Add the prefix 0x to the output of Class X;
The decimal point is given for Class E, G, and F when the result has decimal places.

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.

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.

# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char C, S [20];
Int A = 1234;
Float F = 3.141592653589;
Double X = 0.12345678912345678;
Strcpy (S, "Hello, world ");
C = '\ x41 ′;
Printf ("A = % d \ n", a); // output according to the decimal integer format, display a = 1234
Printf ("A = % d % \ n", a); // output the % result a = 1234%
Printf ("A = % 6D \ n", a); // outputs a space complement to the left of a 6-digit decimal integer. The result is a = 1234.
Printf ("A = % 06d \ n", a); // the output value is 0 on the left of the 6-digit decimal integer, and the result is a = 001234.
Printf ("A = % 2D \ n", a); // A is over 2 bits, according to the actual output A = 1234
Printf ("A = %-6D \ n", a); // outputs a space complement to the right of a 6-digit decimal integer, displaying a = 1234
Printf ("F = % F \ n", f); // The valid floating point number is 7 digits, and the result is f = 3.141593.
Printf ("F = 6.4f \ n", f); // output 6 columns, four digits after the decimal point, result F = 3.1416
Printf ("x = % lf \ n", x); // output long floating point number x = 0.123457
Printf ("x = % 18.16lf \ n", x); // output 18 columns, 16 decimal places, x = 0.1234567891234567
Printf ("C = % C \ n", c); // output character c =
Printf ("C = % x \ n", c); // ASCII code C = 41 that outputs characters in hexadecimal notation
Printf ("S [] = % s \ n", S); // output array string s [] = Hello, world
Printf ("S [] = % 6.9s \ n", S); // output string s [] = Hello, wor
Return 0;
}

Printf output format Summary

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.