%-0 m.n l or h format characters
The following is a description of the items that make up the format description:
①%: the opening symbol of format description, which is essential.
②-:Yes-indicates left-aligned output; If omitted, it means right-aligned output.
③0: 0 means that the specified space is filled with 0, and if omitted, it means that the specified space is not filled.
④ m.n: m refers to the domain width, that is, the number of characters occupied by the corresponding output item on the output device. N refers to accuracy. The number of decimal places used to describe the output real number. For numeric types, when n is not specified, the implied precision is n=6 bits.
⑤l or h:l means long type of integer and double type of real type. H is used to change the format character of an integer to a short integer.
-
layout/format character
Format characters are used to specify the data type and output format of the output item.
①d format: used to output decimal integers. There are the following usages:
%d: Output according to the actual length of integer data.
% md: m is the width of the specified output field. If the number of digits of data is less than m, the left end is filled with spaces; If it is greater than m, it is output according to the actual number of digits.
%ld: output long integer data.
②o format: output integers in unsigned octal format. Long integers can be output in "%lo" format. You can also specify the width of the field output in "%mo" format.
Example:
Master ()
{ int a =- 1;
printf("%d,%o ",a,a);
}
Operation results:-1, 177777
Program analysis:-1 (stored in the form of complement) in the memory unit is (111111/.
③x format: output integers in unsigned hexadecimal form. Long integers can be output in "%lx" format. You can also specify the field width and output it in "%mx" format.
④u format: output integers in unsigned decimal form. Long integers can be output in "%lu" format. You can also specify the field width to output in "%mu" format.
⑤c format: output a character.
⑥s format: used to output a string. There are several uses.
%s: For example, printf("%s ","CHINA ") outputs the" CHINA "string (excluding double quotation marks).
%ms: The output string occupies m columns. If the length of the string itself is greater than m, it will break through the limit of obtaining m and output all strings. If the string length is less than m, the left blank is filled.
%-ms: If the length of the string is less than m, the string will be left and right filled with spaces within the range of m columns.
%m.ns: The output occupies m columns, but only the left n characters in the string are taken. These n characters are output on the right side of column M, with spaces on the left. Note: If n is not specified, it defaults to 0.
%-m.ns: where m and n have the same meaning, n characters are output on the left side of the range of m columns, and spaces are filled on the right side. If n>m is used, the value of n is automatically taken, which ensures the normal output of n characters. Note: If n is not specified, it defaults to 0.
If it is sprintf(desc, "%m.ns", acid); If desc has enough space, null characters will be automatically added to the end of the %m.ns string, unlike strncpy.
For example: sprintf (desc, "%.3s", "123456"); Desc if space >; =4 bytes, and the fourth byte will be a null character.
⑦f format: used to output real numbers (including single precision and double precision) in decimal form. There are the following usages:
%f: No width is specified, all integer parts are output, and 6 decimal places are output.
%m.nf: the output * * * occupies m columns, including n decimal places, such as the left blank with a value width less than m.
%-m.nf: the output * * * occupies n columns, including n decimal places, for example, the width of the value is less than the right end of m to fill in spaces.
⑧e format: output real numbers in exponential form. The following forms can be used:
%e: The number part (also called mantissa) outputs 6 decimal places, and the exponent part occupies 5 or 4 digits.
The characters %m.ne and %-m.ne: m, n and "-"have the same meanings as before. Where n refers to the number of decimal places in the digital part of the data, and m refers to the width occupied by the whole output data.
⑨g format: automatically select the shorter output of F format or E format, and do not output meaningless zeros.
-
Further explanation of printf function;
If you want to output the character "%",you should use two consecutive% in the "format control" string, for example:
printf("%f%% ", 1.0/3);
The output is 0.333333%.
-
For single-precision numbers, only the first 7 digits are valid, and the decimal number is 6 digits.
For double-precision numbers, the first 16 bits are significant and have 6 decimal places when they are output with %lf format symbols.
-
The printf () function is a format output function, and the instruction to request printf () to print variables depends on the type of variables. For example, the% d symbol is used to print integers and the% c symbol is used to print characters. These symbols are called conversion instructions because they specify how to convert data into a displayable form. The following are various conversion instructions provided by the ANSI C standard peintf ().
Conversion instruction and result print out% a floating point number, hexadecimal number and p symbol (C99)
% A floating point number, hexadecimal number and p notation (c99)
%c one character
%d signed decimal integers
%e floating-point number, electronic notation
%E floating-point number, electronic notation
%f floating-point number, decimal notation
%g automatically selects% f or% e according to the value.
%G automatically selects% f or% e according to the value.
%i signed decimal number (same as% d)
%o unsigned octal integer
%p pointer
%s string
%u unsigned decimal integers
%x unsigned hexadecimal integers with hexadecimal number 0f.
%X unsigned hexadecimal integers with hexadecimal number 0f.
%% Print percent sign
//There is also a special format% *. *, the values of these two asterisks are specified by the values of the second and third parameters respectively ("%. * s \ n ",8," abcdefgggggg ");
printf("%*。 *f \n ",3,3, 1.25456 f); Use the basic form of printf () function printf (): printf ("format control string", variable list); # include & ltcstdio & gtint main()
{
//for int
int I = 30 122 12 1;
long i2 = 309095024l
Short i3 = 30
Unsigned i4 = 2123453; printf("%d,%o,%x,%X,%ld,%hd,%u\n ",I,I,I,i2,i3,i4); //If it is: %l, %h, the result cannot be output.
printf("%d,%ld\n ",I,I2); //Cannot test the difference between %ld and %d because long is 4 bytes.
printf("%hd,%hd\n\n ",I,i3); //The difference between %hd and %d was tested because short is 2 bytes.
//For strings and characters
char ch 1 = ' d ';
Unsigned character CH2 =160;
Char *str= "Hello everyone!" ;
printf("%c,%u,%s\n\n ",ch 1,ch2,str); //Unsigned characters exceeding 128 have no corresponding characters.
//For float and double, unsigned and signed cannot be used with double and float.
Float fl = 2.566545445F// or 2.566545f.
double dl = 265.565 1445;
Long double DL2 = 2.5654441454; //%g has no E format, and the default 6 digits include digits before the decimal point.
//%f has no E format, and the default 6 digits only contain 6 digits after the decimal point.
//%e adopts E format, and the default 6 digits are 6 digits after the decimal point after conversion.
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 ",fl,dl); //for point
IP = &
Char * IP 1 = new char;
Void * iP2// Danger!
printf("%p,%p,%p\n\n ",iP,iP 1,iP2);
//Other knowledge: the minus sign indicates left alignment (right alignment by default); %6.3, where 6 represents width and 3 represents precision.
char *s="Hello world!" ;
printf(":% s:\ n:% 10s:\ n:% . 10s:\ n:%- 10s:\ n:%- 15s:\ n:% 15. 10s:\ n:%- 15. 10s:\ n \ n ",
s,s,s,s,s,s,s,s,s); double ddd = 563.908556444
printf(":% g:\ n:% 10g:\ n:% . 10g:\ n:%- 10g:\ n:%- 15g:\ n:% 15. 10g:\ n:%- 15. 10g:\ n \ n ",
ddd,ddd,ddd,ddd,ddd,ddd,ddd,ddd,DDD);
//There is also a special format% *. *, the values of these two asterisks are specified by the values of the second and third parameters respectively ("%. * s \ n ",8," abcdefgggggg ");
printf("%*。 *f \n ",3,3, 1.25456 f); Returns 0;
}