Linux Programming: printf command

Source: Internet
Author: User
Tags print format printf linux

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

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.