Some formats for printf output strings

Source: Internet
Author: User
Tags truncated

1. Output string AS-is:
printf ("%s", str);

2. Output a string of the specified length, long without truncation, and right-justified when insufficient:
printf ("%ns", str); --n 10 binary values for the specified length

3. Output string of specified length, long without truncation, left justified when insufficient:
printf ("%-ns", str); --n 10 binary values for the specified length

4. Output a string of the specified length, truncated long, and right-justified when insufficient:
printf ("%n.ms", str); --n for the final string output length
--m is the length of the substring taken from the argument string

5. Output string of specified length, long truncation, left justified when insufficient:
printf ("%-n.ms", str); --n for the final string output length
--m is the length of the substring taken from the argument string

Note that the so-called super-long truncation of the M is not only a long time to play a role, but whether you are not long, you have to intercept so long. So
The output of printf ("%-5.2", "123") is:
12 Spaces Space Spaces
Only 2 characters are intercepted, others are filled with blanks, and left-justified.

6. The above n,m can be dynamically specified by substituting * instead of M or N, and then adding a numeric parameter to the parameter list. Example:
printf ("%-*.*s", 5, 2, "123"), as in the example above.
printf ("%*s", 5, "123"), indicates that the output length is 5, if the extra length is not truncated, enough words to fill, right to align.

Some formats for printf output strings

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.