printf () formatted output detailed

Source: Internet
Author: User
Tags sprintf

%-0 M.N L or H format characters
The following describes the items that comprise the format description:
①%: 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 precision. The number of decimal digits used to describe the actual number of outputs. For numeric types, when n is not 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 characters
The format character is used to specify the data type and output format of the output item.
①d format: Used to output decimal integers. There are several ways to use this:
%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.
②o format: outputs integers 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.
Cases:
Main ()
{int a =-1;
printf ("%d,%o", A, a);
}
Run Result: -1,177777
Program parsing:-1 in the memory unit (stored in complement form) is (1111111111111111) 2, converted to octal number is (177777) 8.
③X format: outputs 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.
④u format: outputs integers 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.
⑤C format: outputs one character.
⑥s format: Used to output a string. There are several uses.
%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, left to fill the blanks, note: If n is not specified, the default is 0.
%-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, i.e. n characters are guaranteed to output normally, note: If n is not specified, the default is 0.
If it is sprintf (desc, "%m.ns", sour), if the DESC space is sufficient, it will automatically fill the null character at the end of the%M.NS string, unlike the strncpy.
For example: sprintf (desc, "%.3s", "123456"); desc If the space >=4 bytes, the 4th byte will be a null character.
⑦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.
---------------------------------------
A further explanation of the printf function:
If you want to output the character "%", you should indicate a continuous two% in the format control string, such as:
printf ("%f%%", 1.0/3);
Output 0.333333%.
---------------------------------------
For single-precision numbers, when using the%f format character output, only the first 7 digits are valid digits, and the decimal 6 bits.
For double-precision numbers, when using the%LF format character output, the first 16 digits are valid digits, and the decimal 6 bits.
------------------------------------------------------------------
The printf () function is a format output function that requests printf () to print a variable's instruction depending on the type of the variable. For example, when you print an integer that is using the%d symbol, the print character is used with the%c symbol. These symbols are referred to as conversion instructions. Because they specify how no data is converted into a form that can be displayed. The following is a list of the various conversion instructions provided by ANSI C standard printf ().
Conversion description and printout as result%a floating-point, hexadecimal-digit, and P-notation (C99)
%A floating-point numbers, hexadecimal digits, and P-notation (C99)
%c one character
%d signed decimal integers
%e floating-point number, E-notation method
%E floating-point number, E-notation method
%f floating-point number and decimal notation method
%g automatically selects%f or%e depending on the value.
%G automatically selects%f or%e depending on the value.
%i Signed decimal number (same as%d)
%o unsigned octal integer
%p pointer
%s string
%u unsigned decimal integer
%x uses a hexadecimal digit of 0f unsigned hexadecimal integer
%x uses a hexadecimal digit of 0f unsigned hexadecimal integer
Print a percent percent
There is also a special format%*.*, where the values of the two asterisks are specified by the values of the second and third parameters, printf ("%.*s \ n", 8, "ABCDEFGGGGGG");
printf ("%*.*f \ n", 3,3, 1.25456f); The basic form of printf () using the printf () function: printf ("Format control string", variable list); #include <cstdio>int Main ()
{
for int
int i=30122121;
Long i2=309095024l;
Short i3=30;
unsigned i4=2123453; printf ("%d,%o,%x,%x,%ld,%hd,%u\n", i,i,i,i,i2,i3,i4);//If it is:%l,%h, the result will not be lost
printf ("%d,%ld\n", i,i2);//The difference between%ld and%d is not tested because long is 4byteswww.yztrans.com
printf ("%hd,%hd\n\n\n", i,i3);//test the difference between%HD and%d, because short is 2bytes
for string and Char
Char ch1= ' d ';
unsigned char ch2=160;
Char *str= "Hello everyone!";
printf ("%c,%u,%s\n\n\n", ch1,ch2,str);//unsigned Char more than 128 no characters corresponding
For float and double,unsigned and signed can is used with double and float
Float fl=2.566545445f;//or 2.566545445f
Double dl=265.5651445;www.jamo123.com
a long double dl2=2.5654441454;
%G has no e format, the default 6-bit includes the number preceding the decimal point,
%F has no e format, the default 6-bit only contains 6 digits after the decimal point
%e in e format, default 6 bits is 6 digits after the converted decimal point
printf ("%f,%e,%g,%.7f\n", FL,DL,DL,DL);
printf ("%f,%e,%g,%f\n", FL,DL,DL,DL);//%f is wrong
printf ("%.8f,%.10e\n", FL,DL);
printf ("%.8e,%.10f\n\n\n", FL,DL);
For Point
int *ip=&i;
Char *ip1=new char;
void *ip2;//dangerous!
printf ("%p,%p,%p\n\n\n", IP,IP1,IP2);
Additional knowledge: Negative sign, left-aligned (default is right-aligned),%6.3,6 = width, 3 for precision
Char *s= "Hello world!";
printf (":%s: \n:%10s: \n:%.10s: \n:%-10s: \n:%.15s: \n:%-15s: \n:%15.10s: \n:%-15.10s:\n\n\n",
s,s,s,s,s,s,s,s); double ddd=563.908556444;
printf (":%g: \n:%10g: \n:%.10g: \n:%-10g: \n:%.15g: \n:%-15g: \n:%15.10g: \n:%-15.10g:\n\n\n",
DDD,DDD,DDD,DDD,DDD,DDD,DDD,DDD);
There is also a special format%*.*, where the values of the two asterisks are specified by the values of the second and third parameters, printf ("%.*s \ n", 8, "ABCDEFGGGGGG");
printf ("%*.*f \ n", 3,3, 1.25456f); return 0;
}

printf () formatted output detailed

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.