Different types of data have different printing formats. Being familiar with the different output formats of printf will make subsequent learning more effective.
Article directory
-
-
- ==%d%i prints decimal integer data==
- ==flags==
- ==%u print unsigned decimal==
- ==%o prints unsigned octal==
- ==%x %XPrint uppercase or lowercase hexadecimal==
- ==%f prints floating point numbers==
- ==%e %EScientific notation printing==
- ==%gFilter %f redundant 0==
- ==%cPrint character type==
- ==%s print characters ==
- ==%pPrint the address of the incoming variable==
- ==%%Print%==
- ==Print colored fonts==
-
The form of printf function is as follows
int printf(const char8formation,…);
该函数的功能是将格式化的数据打印到输出端。
The conversion specifiers are as follows
and explained in detail one by one (for details on using them with escape characters, seeEscape characters)
%d%i prints decimal integer data
flags
- Minus sign: The result is left aligned and spaces are padded on the right. The default is right alignment, with spaces on the left.
The number in front of d controls the printing width.
- + plus sign: output positive and negative signs
- Space: When the output is positive, the positive sign is replaced by a space. If the output is negative, the space is replaced by a negative sign.
For example:
- Pound sign (#): If the printing types are %o, %x, and %X, add prefixes 0, 0x, and 0X.
If type is a,A,e,E,f,g,G, the decimal point must be retained. By default, if you use .0 control to retain 0 digits after the decimal point, the decimal point will not be output. Add the # sign to display the decimal point.
There is also %g in the directory, which does not output extra zeros. Add # to retain 0 after the decimal point.
- 0, number zero
When controlling the print width at the front, change the default space filling to 0 filling
For example
%u prints unsigned decimal
If you use %u to print a negative number, the compiler will treat the binary of the negative number as a positive number. The value range of signed int is -231< a i=2> to 231-1, that is, -2147483648~2147483647, while the representation range of unsigned int is 0 to 232-1, the binary representation of -1 in memory is 11111111111111111111111111111111. For unsigned, the first bit is no longer the sign bit, which represents the maximum unsigned integer value. Therefore, the printed result is 4294967295.
%o print unsigned octal
%x %X prints uppercase or lowercase hexadecimal
still unsigned
%f prints floating point numbers
Add a dot (.) before f and a number after it to indicate how many decimal places to keep.
It will be rounded automatically.
You can also use an asterisk (*) to save the number of digits. The format is as follows (not commonly used, just understand it)
This method of shaping can also be used, followed by 3 and 6 is the width to print.
You should know here that %f retains 6 digits after the decimal point by default
If you want to obtain higher precision, you must specify its precision
%e %EScientific notation printing
Only serving floating point numbers, unknown errors (data errors) will occur when printing integers.
%g filters %f extra 0s
%f retains 6 digits after the decimal point by default. If the extra 0 looks irritating, you can directly use %g to print without the trouble of controlling the width.
Through the C++ Reference, you can see that the introduction of %g is to select %f or %e according to the number to control the shortest output and not output excess 0. You can see that the example above selects % The form of f (and no extra 0s are printed)
Look at the example below
The number above requires 8 digits to print with %f, while %e only requires 7 digits. digits. The following number %f only requires 7 digits, while %e requires 8 digits. Is it very obvious in comparison?
%c print character type
This is very simple.
Compared with the ASCII table, 97 represents the character a. If printed in the form of %c, the corresponding character will be found and printed according to the ASCII code table.
%s prints characters
To print with %s, just give a character pointer. The compiler will automatically recognize and print the string for you.
%p prints the address of the incoming variable
Anyone who has studied pointers knows that what a pointer saves is the address pointing to a variable. %p prints the pointer in hexadecimal.
Use it with the address operator (&), Review the review operator?
%%Print%
Use two % to print %, otherwise when printing %, it may be combined with the following characters, and the compiler will think it is an identifier.
I just want to print heihei%c to the monitor. If I write it like this, it will report an error, so I need to use two %
as shown in the picture
%a%A hexadecimal output floating point number
%a and %A are formatting types introduced by C99, which print floating-point numbers in hexadecimal form.
The binary number of 2.5 is 0010 1000..., shift the corresponding binary number to the right by one position and the first letter is 1 (it is always 1, if it is 0110 1000, then move it to the right by two places and keep the first The bit is 1)
Why use such a complicated representation method? Numbers like 0.5 and 2.5 can all be expressed accurately, but 0.15 cannot be expressed accurately. The reason can be seen in the second questionBrushing Notes.
Using this representation method, for example, 15.15 can be expressed more accurately.
The analysis is as follows
Print colored fonts
The default printing in C language is white. Sometimes in order to highlight specific information and avoid important warning or error information being overwhelmed by other large amounts of printed information, you can change the color of the output font or highlight the background.
format
1,printf(“\033[font background color, font color m string\033[0m”)
The following are the corresponding numbers for the colors
background color | font color |
---|---|
40: black | 30: black |
41: red | 31: red |
42: green | 32: green |
43: yellow | 33: yellow |
44: blue | 34: blue |
45: Purple | 35: Purple |
46: dark green | 36: dark green |
47: white | 37: white |
For example
This article ends here. If there is anything you need to add, you can leave your comments in the comment area.