Fldwidth and precision parsing of conversion instructions for formatted output in C language

Source: Internet
Author: User

The first thing to say is the formatted output of the C language, which is the printf and several variants of it (grep-e "v?"). (sn|s|f) printf "). Like these functions have a parameter Format,format can add some conversion instructions, let the output more regular! And this conversion description is made up of these parts:

%[flags][fldwidth][precision][lenmodifier]convtype

This article focuses on fldwidth and precision parts, other parts can refer to "C Primer Plus" Fifth Edition 4.4 section!

First of all, the Fldwidth, which is called field width, is the minimum width of the final converted character. Note the smallest adjective, it means that it is not enough to fill the space or 0, many more, not cut short!

And that precision represents precision, starting with a decimal point. On its role, Apue (5.11 Section 1) on the above, precision shows the number of digits after the integer conversion, the least number of digits after the conversion of floating-point number, the maximum number of characters after the string conversion! The smallest of the floating-point numbers (the blue part of my logo) I feel a bit doubtful, because if the floating-point number exceeds that value it will be truncated!

OK, here's the theory, then we'll cook a chestnut! For example, the following code:

  

First of all, the floating-point printf, the number before the decimal point is Fldwidth, which indicates the final overall width is 11 characters in width. The 3 is precision, which means that the floating-point number retains 3 digits after the decimal point, so that 4 is rounded! Theoretically should output 5223.332, but here the output of a 5223.333, here I have not thought to understand, who if know the inside, but also look at the liberal enlighten!

Then look at that integer number of printf, here fldwidth or 11, so the final overall width and the above the same, here the precision into 8, that is, the integer number at least to output 8 bits, but not enough to do, that in front of 0 Ah! So the final result we see is 00030000!

Finally see that string of printf, here fldwidth and above the same, the precision is 8, here that the output of the string is the longest can only be 8, but I this str very disappointing, here unexpectedly over, no way, had to cut short! So hello,world! became a hello,wo!

OK, this is about the format conversion description of Fldwidth and precision is these, here because my level is limited, that floating point rounding has not wanted to understand, later want to understand again to correct! Also hope forgive me!

Fldwidth and precision parsing of conversion instructions for formatted output in C language

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.