C ++ formatted strings are often used to format the output of numbers, merge and convert strings, and so on.
1.Formatting Operator
When there are too many threads, there are too many threads, too many threads.
SymbolFunction
── ─
% DDecimal signed integer
% UUnsigned decimal integer
% FFloating Point Number
% SString
% CSingle Character
% PPointerValue
% EExponential floating point number
% X, % xUnsigned hexadecimal integer
% 0Unsigned integer in octal format
% GAutomatically select the appropriate representation
When there are too many threads, there are too many threads, too many threads.
Description:
(1 ).You can"%"Enter a number between and to indicate the largest field width.
For example: % 3dOutput3Integer,Not enough3Right-aligned.
% 9.2fIndicates that the output field width is9Floating Point Number,The decimal point is2,The integer is6,
One decimal point,Not enough9Right-aligned.
% 8 sOutput8Character String,Not enough8Characters.
If the length of the string, or the number of integer digits exceeds the field width,The output is based on the actual length. However,If the integer part of the number of digits exceeds the width of the specified integer,Will be output by the actual integer;If the number of digits in the fractional part exceeds the specified decimal point width,The output is rounded to the specified width.
In addition,If you want to add some0,Add one before the wide field0.
For example: % 04dIndicates that the output value is smaller4Bit Value,Will be supplemented in front0Set the total width4Bit.
If a floating point is used to represent the output format of a character or integer,The number after the decimal point represents the maximum width.,The number before the decimal point represents the minimum width.
For example: % 6.9 sIndicates that a length is not less6And not greater9. If the value is greater9,Then9The contents after the characters are deleted.
(2 ).You can"%"And lowercase lettersL,Indicates that the output is a long number.
For example: % LDOutputLongInteger
% LfOutputDoubleFloating Point Number
(3 ).You can control the Left or Right alignment of the output.,That is"%"Add"-"The output is left aligned.,Otherwise, it is right aligned.
For example: %-7dOutput7Bitwise integer left alignment
%-10 sOutput10Characters left aligned
2.Some special characters
When there are too many threads, there are too many threads, too many threads.
CharacterFunction
── ─
\ NLine feed
\ FClear screen and change pages
\ REnter
\ T TabCharacter
\ XhhRepresentsASCIICode usage16Forward Representation,WhereHHYes1To2Items16Hexadecimal number
When there are too many threads, there are too many threads, too many threads.
Char C, S [20], * P;
Int A = 1234, * I;
Float F = 3.141592653589;
Double X = 0.12345678987654321;
P = "How do you do ";
Strcpy (S, "Hello, comrade ");
* I = 12;
C = '\ x41 ';
Printf ("A = % d \ n", );/*Returns a decimal integer.A = 1234 */
Printf ("A = % 6D \ n", );/*Result output6Decimal numberA = 1234 */
Printf ("A = % 06d \ n", );/*Result output6Decimal numberA = 001234 */
Printf ("A = % 2D \ n", a);/*Exceeds2Bit,Output by actual valueA = 1234 */
Printf ("* I = % 4D \ n", * I );/*Output4Decimal integer* I = 12 */
Printf ("* I = %-4D \ n", * I );/*Left alignment of output4Decimal integer* I = 12 */
Printf ("I = % P \ n", I );/*Output addressI = 06e4 */
Printf ("F = % F \ n", f );/*Output floating point numberF = 3.141593 */
Printf ("F = 6.4f \ n", f );/*Output6Decimal point4Bit floating point numberF = 3.1416 */
Printf ("x = % lf \ n", x );/*Output long floating point number* = 0.123457 */
Printf ("x = % 18.16lf \ n", x );/*Output18Decimal point16Long Floating Point Number of BITs* = 0.1234567898765432 */
Printf ("c = % C \ n", c );/*Output charactersC = */
Printf ("c = % x \ n", c );/*Output characterASCIICode valueC = 41 */
Printf ("s [] = % s \ n", S );/*Output array stringS [] = Hello, Comrade */
Printf ("s [] = % 6.9s \ n", S );/*Maximum output9Character StringS [] = Hello, Co */
Printf ("s = % P \ n", S );/*First character address of the output array stringS = ffbe */
Printf ("* P = % s \ n", P );/*OutputPointerStringP = How Do You do */
Printf ("P = % P \ n", P );/*OutputPointerValue(P = 0194 */
The address values in the preceding results may be different on different computers.
From: http://www.henryxu.com/post/1.html