Complete output format and color font printing of C language printf

Insert image description here
Different types of data have different printing formats. Being familiar with the different output formats of printf will make subsequent learning more effective.


The form of printf function is as follows

int printf(const char8formation,…);

该函数的功能是将格式化的数据打印到输出端。
The conversion specifiers are as follows
Insert image description here
and explained in detail one by one (for details on using them with escape characters, seeEscape characters)

%d%i prints decimal integer data

Insert image description here

flags

Insert image description here

  • 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.

Insert image description here

  • + plus sign: output positive and negative signs

Insert image description here

  • 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:
Insert image description here

  • Pound sign (#): If the printing types are %o, %x, and %X, add prefixes 0, 0x, and 0X.

Insert image description here

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.
Insert image description here

There is also %g in the directory, which does not output extra zeros. Add # to retain 0 after the decimal point.
Insert image description here

  • 0, number zero

When controlling the print width at the front, change the default space filling to 0 filling
For example
Insert image description here

%u prints unsigned decimal

Insert image description here
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

Insert image description here

%x %X prints uppercase or lowercase hexadecimal

still unsigned
Insert image description here

%f prints floating point numbers

Add a dot (.) before f and a number after it to indicate how many decimal places to keep.
Insert image description here
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)
Insert image description here
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
Insert image description here
If you want to obtain higher precision, you must specify its precision
Insert image description here

%e %EScientific notation printing

Insert image description here
Only serving floating point numbers, unknown errors (data errors) will occur when printing integers.

%g filters %f extra 0s

Insert image description here
%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
Insert image description here
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.
Insert image description here
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

Insert image description here
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.
Insert image description here
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.
Insert image description here
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
Insert image description here
%a%A hexadecimal output floating point number
%a and %A are formatting types introduced by C99, which print floating-point numbers in hexadecimal form.
Insert image description here
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)
Insert image description here
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.
Insert image description here
The analysis is as followsInsert image description here

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
Insert image description here
This article ends here. If there is anything you need to add, you can leave your comments in the comment area.

Guess you like

Origin blog.csdn.net/qq_75270497/article/details/134857961