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

Source: Internet
Author: User

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

What is the format output of C language, that is, printf and several of its variants (grep-E "v? (Sn | s | f) printf "). For example, all these functions have a format parameter. You can add some conversion instructions in format to make the output more regular! The conversion is composed of the following parts:

% [Flags] [fldwidth] [precision] [lenmodifier] convtype

This article focuses on the fldwidth and precision sections. For other sections, see section 4.4 of C Primer Plus version 5!

First, the fldwidth is called field width, which is the minimum width of the converted characters. Pay attention to the least adjective, which means that it is not enough to fill in space or 0. If it is more, it will be more and it will not be truncated!

The precision indicates the precision, starting with a decimal point. About its function, APUE (section 1 of section 5.11) is like this. precision indicates the minimum number of digits output after Integer Conversion and the minimum number of digits after the decimal point after floating point conversion, the maximum number of characters After String Conversion! I have some questions about the smallest floating point number (the blue part I marked), because if the floating point number exceeds that value, it will be truncated!

Okay, that's the theory. Let's cook a chestnut next! For example, the following code:

  

First, the floating point printf. the number before the decimal point is fldwidth, indicating that the final overall width is the width of 11 characters. That 3 is precision, which indicates that the floating point number is retained three places after the decimal point, so that 4 is rounded up! In theory, 5223.332 should be output, but a 5223.333 should be output here. I haven't figured out which of the following can be used for further understanding!

Let's take a look at the printf of the integer. fldwidth is still 11 here, so the overall width is the same as that above. The precision here is changed to 8, that is, the integer number must output at least eight digits, but it's not enough to do it. Make up for zero in front! So the final result we see is 00030000!

Finally, let's take a look at the printf of the string. Here fldwidth is the same as above, and the precision is 8. Here, it indicates that the output string can only be 8 at the longest, but I am not so angry with this str. It exceeded here, no way, you have to truncate it! So Hello, World! It becomes "Hello" and "woo!

 

OK, this is the fldwidth and precision about the format conversion instructions. Here, due to my limited level, I still don't want to understand the rounding of the floating point number. I want to understand it later and try again! Sorry!

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.