Formatted output and input for 06-c languages

Source: Internet
Author: User

The printf () function is a formatted output function that is typically used to output information to a standard output device in a prescribed format. The printf () function is called in the format: printf ("< formatted string >", < Parameter table >);
Where the formatted string consists of two parts: some are normal characters, these characters will be output as-is, and the other part is the formatting of the specified character, starting with "%", followed by one or several specified characters, used to determine the output content format.
The parameter table is a series of parameters that need to be output, the number must be as many as the number of output parameters as indicated by the formatted string, the parameters are separated by "," and the order one by one corresponds, otherwise unexpected errors will occur.
The full format of printf format control:%-0 M.N L or H-format characters are described in terms of the constituent format descriptions:
①%: Represents the starting symbol for the format description, which is indispensable.
②-: Yes-Indicates left-aligned output, such as omitting to indicate right-aligned output.
③0:0 indicates that the specified vacancy is filled in 0, as omitted to indicate that the specified space is not filled.
④m.n:m refers to the width of the field, which is the number of characters that the corresponding output item occupies on the output device. n refers to the precision used to describe the number of decimal digits of the output's real number. When n is specified, the implied precision is n=6 bit.
⑤l or h:l refers to the integer type long, which refers to the double type of the real type. H is used to Fu Xiu the format character of an integral type to a short type.
Format Summary:
(1) The most commonly used format is%d, meaning that an integer is printed in 10 binary form. If the output integer is negative, the first character of the output is the-number.
(2) The%u format is similar to the%d format, except that it requires the printing of unsigned 10 binary integers. Outputs an integer in unsigned decimal form. The long integer type can be output in the format "%lu". You can also specify the field width to be output in "%MU" format.
(3)%o format request output 8 integer, output integer in unsigned octal form.
(4)%x and%x format request Output 16 integer. The%x format uses lowercase letters a,b,c,d,e,f to represent numbers between 10 and 15, outputting integers in unsigned hexadecimal form. The%x format uses the capital letter a,b,c,d,e,f to denote a number from 10 to 15 note: 8 and 16 binary integers are always treated as unsigned numbers.
(5) The%s format is used to print a string, and the corresponding parameter should be a character pointer, and the character to be output starts at the address pointed to by the pointer, until a null character (' s ') is present to terminate. %s: For example: printf ("%s", "China") output the "China" string (excluding double quotes). %ms: The output of the string in the M-column, such as the string itself is greater than M, then the limit of M, the string is all output. If the string length is less than M, the left fill space. %-ms: If the string length is less than M, in the range of M columns, the string is left and the right is the space. %M.NS: The output occupies M-column, but only the left-hand side of the string is n characters. The n characters output to the right of the M column and left to fill the blanks. -m.ns: where m and n mean the same, n characters output to the left of the M-column range and right-fill the space. If n>m, the N value is automatically taken, that is, the normal output of n characters is guaranteed.
(6) The%c format is used to print a single character:
(7)%g,%f and%e These three formats are used to print floating-point values. The%g format is especially useful for printing floating-point numbers that do not need to be aligned by column. Its role is 2:1, remove the number of excess 0 (not reaching six digits) two, reserved six digits (extra six bits)%e format used to print floating-point numbers, display the use of exponential form: For example: Output pi is: 3.141593e+00 difference between the two: The number printed in G format is a total of 6 valid digits%e format 6 digits after the decimal point is printed out%f prohibit the use of exponential form to represent floating-point numbers. So the PI output is: 3.141593 (but note the accuracy requirement: 6 digits after the decimal point)
(8) The percent format is used to print a% character.
(9)%e and%g are only used in uppercase letters (e) in place of the lowercase letter (e) ⑦f format: for outputting real numbers (including single and double precision) and outputting them in decimal form. There are several uses:%f: Do not specify a width, the integer part outputs and outputs 6 decimal places. %M.NF: The output is a total of M columns, where there are n decimal places, such as the value width is less than the left fill space. %-M.NF: The output is a total of n columns, where there are n decimal places, such as the value width is less than m right end fill space. ⑧e format: Outputs real numbers in exponential form. The following forms are available:%e: The digital part (also called the mantissa) outputs 6 decimal places, and the exponent portion is 5 bits or 4 bits. %m.ne and%-m.ne:m, N, and "-" characters have the same meaning as before. Here n refers to the number of decimal digits of the data, and m represents the width of the entire output data. ⑨g format: Automatically select a shorter output in f format or e format, and do not output meaningless 0.
(1). You can insert a number between "%" and a letter to indicate the maximum field width. For example:%2d represents the output of 3-bit integers, not enough 2-bit right-justified. For example:%5d represents the output of 3-bit integers, not enough 5-bit right-justified. For example:%10d represents the output of 3-bit integers, not enough 10-bit right-justified. unsigned int i=295; printf ("%2d\n", I); printf ("%5d\n", I); printf ("%10d\n", I); 295 295 295 Press any key to continue (2). 0 or other example:%02d indicates output 3-bit integer, not enough 2-bit right-aligned, 0. For example:%05d represents the output of 3-bit integers, not enough 5-bit right-justified, 0. For example:%010d represents the output of 3-bit integers, not enough 10-bit right-justified, 0. unsigned int i=295; printf ("%02d\n", I); printf ("%05d\n", I); printf ("%010d\n", I); 295 00295 0000000295 Press any key to continue (3). negative int i=-295; printf ("%02d\n", I); printf ("%05d\n", I); printf ("%010d\n", I); -295-0295-000000295 Press any key to continue
(4). You can control the output left or right alignment, that is, "%" and the letter by adding a "-" sign to indicate that the output is left-justified, otherwise it is right-justified. unsigned int i=295; printf ("%-02d\n", I); printf ("%-05d\n", I); printf ("%-010d\n", I); 295 295 295 Press any key to continue
(5). You can add a lowercase letter L between "%" and the letter, indicating that the output is a long form. For example:%ld means output long integer%LF indicates output double floating-point number
(6).%9.2f represents a floating-point number with an output field width of 9, where the decimal bit is 2, the integer digit is 6, the decimal point is one digit, and not enough 9-bit right-aligned. For example,%6.9s indicates that a string with a length of not less than 6 and not greater than 9 is displayed. If it is greater than 9, the contents of the 9th character will be deleted.

Formatted output and input for 06-c languages

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.