C language printf () format Output Control

Source: Internet
Author: User

Printf () is a formatting output function, which is generally used to output information to the standard output device in the specified format. This function is often used in programming. The call format of the printf () function is:

Printf ("<formatted string>", <parameter table> );

The formatted string contains two parts:

  • Some are normal characters, which are output as is.
  • The other part is the formatting rule character, starting with "%", followed by one or more specified characters, used to determine the output content format.

A parameter table is a series of parameters that need to be output. The number of parameters must be the same as the number of output parameters described in the formatted string. Each parameter is separated by a comma (,) and the order is one to one, otherwise, unexpected errors may occur.

Format a string

The format of the formatted string is: % [flag] [minimum output width] [. Precision] [length] format characters

"%-Md": Left alignment. If the m ratio is small, it is output as needed.

"% M. ns": output m-bit. Take the string (from left) n-bit, left fill space, when n> m or m is omitted, m = n. For example, "% 7.2 s" is input to CHINA and "CH" is output ".

"% M. nf": Output floating point number, m is the width, n is the right digit of the decimal point, for example, "% 3.1f", input 3852.99, output 3853.0

Length: h is the short integer, and l is the long integer.

Conversion specifier
  • % A (% A) floating point number, hexadecimal number, and p-(P-) Notation (C99)
  • % C characters
  • % D signed decimal integer
  • % F floating point number (including float and doulbe)
  • % E (% E) floating point number index output [e-(E-) Note]
  • % G (% G) floating point number does not show meaningless zero "0"
  • % I signed decimal INTEGER (same as % d)
  • % U unsigned decimal integer
  • % O octal integer e.g. 0123
  • % X (% X) hexadecimal integer 0f (0F) e.g. 0x1234
  • % P pointer
  • % S string
  • % "%"
Flag
  • Left alignment: "-" e.g. "%-20 s"
  • Right alignment: "+" e.g. "% + 20 s"
  • Space: If the symbol is positive, space is displayed. If the symbol is negative, "-" e.g. "% 6.2f" is displayed"
  • #: No effect on Class c, s, d, and u; prefix o for Class o for output; prefix 0x for Class x for output; and prefix 0x for Class e, class g and Class f give the decimal point only when the result has decimal places.

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.