C Language Output format

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
The following are descriptions 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, such as omitting indicates that the specified vacancy 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.
%d: Output based on the actual length of the integer data.
%md:m The width of the specified output field. If the number of bits in the data is less than M, then the left is a space, and if it is greater than M, the actual number of digits is output.
%ld: Outputs long-integer data.

(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.
The long integer type can be output in the format "%lo". You can also specify the field width to be output in "%MO" format.

(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 long integer type can be output in the format "%LX". You can also specify the field width to be output in "%MX" format.
the a,b,c,d,e,f in the%x format to represent numbers between 10 and 15.
Common denominator: 8 binary and 16 binary integers are always processed 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: for example:
printf ("%c", c); equivalent to Putchar (c);

(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. It has two functions:
One, remove the extra 0 (not up to six digits) of the tail
Two, retain six digits (extra six bits)
The%e format is used to display the exponential form when printing floating-point numbers: For example: Output pi: 3.141593e+00
the difference between the two:
the number printed in%g format is a total of 6 valid digits
%e format to print out 6 digits after the decimal point
%f prohibit the use of exponential form to represent floating-point numbers. So the PI output is: 3.141593
(but note its accuracy requirement: Also 6 digits after the decimal point)
(8) The percent format is used to print a% character.

(9)%e and%g only use capital letters (e) in place of lowercase letters (e) when outputting
⑦f format: Used to output real numbers (including single and double precision), output in decimal form. There are several ways to use this:
%f: Does not specify the 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 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.
/*******************************************************************/
unsigned int i=295;
printf ("%d\n", I);
295
Press any key to continue

(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). Fill 0 or other
For example:%02d represents the output of 3-bit integers, not enough 2-bit right-justified, 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 numbers
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 indicates an output long integer
%LF indicates the output double floating-point number

(6).
The %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, 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.
/*
unsigned int i=295;
printf ("%d\n", I);
printf ("%1d\n", I);
printf ("%09d\n", I);
printf ("%09d\n", (unsigned char) i);
printf ("%9d\n", (unsigned char) i);
printf ("%-9d\n", (unsigned char) i);
*/
/*
295
295
000000295
000000039
the
the
Press any key to continue
*/
/*******************************************************************/
the format for M.N can also be represented as follows (example)
int m=10,n=5;
Char ch[]= "Abcdefghijklmnopqrst";
printf ("%*.*s\n", m,n,ch);//output is ABCDE
The front of the * defines the total width, behind the definition is the number of outputs, respectively, corresponding to the outside of the parameters m and N.
I think the advantage of this approach is that parameters m and n can be assigned outside of the statement to control the output format
/*******************************************************************/
"%08lx\n", 4byte
"%04x\n", 2byte
"%-2.2bx", 1byte

C Language Output format

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.