Printf () and printf

Source: Internet
Author: User

Printf () and printf
If you want to output a mixed product composed of static text and dynamic information stored in one or more variables, the printf () Statement is no longer ideal. There are two reasons why it is ideal. First, it clearly divides static data and dynamic data into two different parts for easy maintenance. Secondly, through printf (), we have full control over how to display dynamic information on the screen, including type, accuracy, alignment and position. Printf ("here there is % d bottle of water. ", 100); execute this command to output: there are 100 bottles of water. In this example, % d is a placeholder called a class indicator, and d indicates that an integer will be placed at this position. When this statement is executed, parameter 100 inserts a placeholder. Keep in mind that an integer is needed. Therefore, if a descendant contains a small number, the number is rounded down to the nearest non-integer. If 100.2 or 100.6 is uploaded, 100 is output. If a string is uploaded, 0 is output. This applies to other types of indicators. Common type indicator type description % B considers the parameter as an integer and displays it as binary % c. The parameter is considered as an integer, displayed as a non-ASCII character % d. The parameter is considered as an integer and displayed as a signed decimal number % f. The parameter is considered as a floating point number, displayed as float % o the parameter is considered as an integer, displayed as octal % s the parameter is considered as a string, displayed as a string % u the parameter is considered as an integer, the value is displayed as unsigned decimal number % x. The parameter is considered as an integer and displayed as lowercase hexadecimal number % y. The parameter is considered as an integer, if the hexadecimal number is displayed in uppercase, If You Want To transmit two values, you only need to insert two specifiers to the string and ensure that the two values are passed as parameters. For example: printf ("% d bottle water cost $ % f", 100, 43.20); execute this command output: 100 bottle water cost $43.20. When processing small numbers, you can use a precision indicator to adjust the accuracy. The following is an example: printf ("%. 2f", 43.2); The above output: 43.20 has some indicators that can be used to adjust the alignment of parameters, fill characters, plus and minus signs, and width.

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.