C + + formatted strings are often used as output for formatted numbers, string merges and transformations, and many more.
1. Formatting the specified character
━━━━━━━━━━━━━━━━━━━━━━━━━━
symbolic effect
──────────────────────────
%d decimal signed integer
%u decimal unsigned integer
%f floating point
%s string
%c Single character
%p The value of the pointer
floating-point numbers in the form of%e indices
%x,%x unsigned integer in hexadecimal notation
%0 unsigned integer represented in octal
%g automatic selection of appropriate representations
━━━━━━━━━━━━━━━━━━━━━━━━━━
Description:
(1). You can insert a number between "%" and a letter to indicate the maximum field width.
For example:%3d represents the output of 3 -bit integers, not enough 3 -bit right-justified.
The%9.2f represents a floating-point number with an output field width of 9, where the decimal bit is 2 and the integer bit is 6.
one decimal point, not enough 9 -bit right-aligned.
The%8s represents the output of a 8 -character string, which is not aligns aligned to 8 characters.
if the length of a string, or the number of integers exceeds the width of the description, it is output at its actual length. But for floating-point numbers, if the integer number of bits exceeds the width of the indicated integer digits, it will be output according to the actual integer digits; if the fractional number of digits exceeds the indicated decimal width, the output is rounded by the width of the description.
In addition, if you want to add some 0 before the output value, you should add a 0 before the field width.
For example ,%04d means that when you output a value that is less than 4 bits, it will be preceded by 0 to make its total width 4 bits.
If you use floating-point numbers to represent the output format of a character or integer, the number after the decimal point represents the maximum width, and the number before the decimal point represents the minimum width.
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.
(2). 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
(3). 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.
Example:%-7d for output 7 -bit integers left justified
%-10s indicates the output of the word aligns alignment
2. Some special provisions of the character
━━━━━━━━━━━━━━━━━━━━━━━━━━
character Action
──────────────────────────
\ nthe line break
\f Clear screen and change page
\ r Enter
\ t Tab character
\xhh represents an ASCII code with an x-input representation, where HH is 1 to 2 decimal digits
━━━━━━━━━━━━━━━━━━━━━━━━━━
Char c, s[20], *p;
int a=1234, *i;
float f=3.141592653589;
Double x=0.12345678987654321;
P= "How does You Do";
strcpy (S, "Hello, comrade");
*i=12;
c= ' \x41 ';
printf ("a=%d\n", a); /* result output decimal integer a=1234*/
printf ("a=%6d\n", a); /* Results Output 6 -bit decimal number a= 1234*/
printf ("a=%06d\n", a); /* Results Output 6 -bit decimal number a=001234*/
printf ("a=%2d\n", a); /*a more than 2 bits, output by actual value a=1234*/
printf ("*i=%4d\n", *i); /* output 4 -bit decimal integer *i= 12*/
printf ("*i=%-4d\n", *i); /* output left-justified 4 -bit decimal integer *i=12*/
printf ("i=%p\n", I); /* output address i=06e4*/
printf ("f=%f\n", f); /* output floating-point number f=3.141593*/
printf ("f=6.4f\n", f); /* output 6 -bit floating-point number with 4 digits after the decimal point f=3.1416*/
printf ("x=%lf\n", X); /* output long floating-point number x=0.123457*/
printf ("x=%18.16lf\n", x);/* outputs The long floating-point number of which the decimal point is x=0.1234567898765432*/
printf ("c=%c\n", c); /* output character c=a*/
printf ("c=%x\n", c); /* ASCII code value for output character c=41*/
printf ("s[]=%s\n", s); /* output array string S[]=hello, comrade*/
printf ("s[]=%6.9s\n", s);/* outputs a string of up to 9 characters s[]=hello,co*/
printf ("s=%p\n", s); /* output array string first character address s=ffbe*/
printf ("*p=%s\n", p); /* output pointer string p=how do do*/
printf ("p=%p\n", p); / * The value of the output pointer p=0194*/
the address values in the above results may be different on different computers.
Transferred from: http://www.henryxu.com/post/1.html
C + + format string description