printf is a command that prints the characters entered from the standard in the format you require to the standard output, which is the screen. In many cases, we may need to format our own data to his output. For example, the output of a test score: Suppose a file Test.txt records the test scores, as shown in the following figure:
The data in the table above is divided into five fields, where the tab or SPACEBAR is used to separate the fields.
printf Command format:
printf ' Print format ' actual content
A few special styles for formatting
Let's do a few common exercises next. Assuming all the data is generic (which is also the most common state), the most common symbol for separating data is Tab. Because the [Tab] key can make the data in a neat arrangement! So how do you use printf commands? Refer to this example below:
As shown above, the output of the printf command is not aligned, and%s represents an unfixed length string, which is handled in the middle of the string with the [tab] delimiter. Since the length of each field is not fixed will cause the above trouble, then I will be fixed on each field is good.
Displays the contents of the Test.txt file in a string, Integer, and decimal point, respectively:
The format above is divided into five fields,%10s represents a 10-character string field,%5i represents a 5-character numeric field, and the%8.2f represents a 8-character field with a decimal point, with a decimal-width of two characters, and a total width of 8 Characters, the integer portion occupies 5 characters, the decimal point itself (.) occupies one digit, and the number of digits after the decimal point is two digits.
In addition to being formatted, printf can display data based on ASCII numbers and character equivalents, for example, what ASCII display characters can be obtained from 16-digit 55?
This article is from the "Soldier" blog, please be sure to keep this source http://ganbing.blog.51cto.com/7002794/1223334