[C Language] Do you really understand the use of printf?

Table of contents

1. What is printf?

2. Format control string and output value parameter table

2.1 Format control string

2.2 Output value parameter table

2.3 List of format characters and format modifiers 

2.3.1 List of format characters

2.3.2 Format modifiers

 3. Detailed explanation of common format characters

 %d

%md 

 %f

%.nf

%m.f

 %c and %s

%c

%s

 6. The return value of printf

5.Common problems in printf

6. Summary 

7. Preface


1. What is printf?

The printf function is a format output function , and the last letter f of its keyword means "format (format)". Its function is to display the specified data on the display screen according to the format specified by the user.

The printf function is a standard library function , and its prototype is in the header file "stdio.h". But as a special case, it is not required to include the stdio.h file before using the printf function. The general form of a printf function call is:

printf("format control string", output table columns) 

printf("f=%f,c=%f\n",f,c);

 Among them, f=%f,c=%f\n  is the format control string , f,c  is the output value parameter list .


2. Format control string and output value parameter table

printf("f=%f,c=%f\n",f,c);

2.1 Format control string

The format control string is a string enclosed in "double quotes" , including three types of information:

  1. format character . Format characters are led by "%", such as %d, %f, etc. Its role is to control the format of the output characters .
  2. escape character . The escape character in the format control string is output according to the escaped meaning , such as the newline character "\n" in the double quotes of the printf function above, that is, the carriage return is output.
  3. ordinary characters . Ordinary characters are the characters that need to be output as they are , such as the "f=" and "c=" parts in the double quotes in the above printf function.

2.2 Output value parameter table

(2) The output value parameter table is a list of data items that need to be output. The output data items can be constants, variables or expressions . The output value parameters are separated by commas , and their types should match the format characters . Each format character is in one-to-one correspondence with the output value parameters in the output value parameter table . If there is no output parameter, the format character is no longer required in the format control string 


2.3 List of format characters and format modifiers 

2.3.1 List of format characters

format character illustrate
d Output a signed decimal integer, the sign of the positive number is omitted
u output as an unsigned decimal integer
o Output as an unsigned octal integer without leading 0
x Output as an unsigned hexadecimal integer (lowercase), without the leading character 0x
X Output as an unsigned hexadecimal integer (uppercase), without the leading character 0X
f Output single and double precision numbers in decimal form, implicitly output 6 decimal places
e Output real numbers in exponential form (lowercase e indicates the exponent part)
E Output real numbers in exponential form (capital E for exponent part)
g Automatically select one of f or e with a smaller output width, and do not output meaningless 0
c output a character
s output string

2.3.2 Format modifiers

format modifier illustrate
english letter l When modifying the format characters d, u, o, x, it is used to output long data
English letter L When modifying the format characters f, e, and g, it is used to output long double data
english letter h When modifying the format characters d, o, x, it is used to output short data
Output domain width m (m is an integer) Specifies the number of columns occupied by the output item
display precision.n (n is an integer) For real numbers, it means to output n decimal places; for strings, it means the number of intercepted characters
- (minus sign)

Output numbers or characters to the left within the field


 3. Detailed explanation of common format characters

 %d

 Output a signed decimal integer, and the sign of a positive number is not output.

int a = 888,b = -666;
printf("%d\n%d",a,b);

 output result:

 You can also insert a format modifier between % and the format character to specify the field width of the output data (the number of columns occupied), such as "%5d", specifying that the output data occupies 5 columns, and the output data is in the field to the right align. like:

%md 

int a = 888, b = -666;
printf("%5d\n%5d", a, b);

output result: 

It can be seen from the results that adding the number 5 between % and d, 888 occupies 3 field widths, and the specified field width > output data length . The output data is on the right, with spaces in front.

What if the specified field width < output data length ?

 Visible will be output as it is.

To output long (long integer) data, add the letter l (representing long) before the format character d, that is, "%ld".


 %f

 Output a real number (including single-precision, double-precision, long double-precision), output in decimal form, there are the following usages:

The length of the output data is not specified, and the system determines the number of columns occupied by the data according to the actual situation of the data. The processing method of the system is generally: all the integer parts in the real number are output, and the decimal part is 6 bits .

Although a is a double type, so a/3 is also a double type. But the %f format character can only output 6 decimal places .


So how to control the number of digits after the output decimal point? 

%.nf

n means to keep n decimal places after the decimal point. as follows

Of course, the floating-point type can also control the domain width, as follows


%m.f

It can be seen that %3.f is actually %3.0f , the front of the decimal point controls the domain width , and the number of decimal points reserved after the decimal point is controlled.


 %c and %s

%c

You can also add format modifiers to specify the field width, such as:

It can be seen that 4 spaces are filled in front of the character a. Because the character a occupies a placeholder.

%s

 


 6. The return value of printf

The most used function when learning C language is the printf function, and the return value of the printf function is ignored by most people, so that many applicants are at a loss when they encounter the programming of the return value of printf, and then come to Talk about the return value of printf.

int A=43;
printf("%d\n",printf("%d",printf("%d",A)));
Seeing the running results and code, can you think of why? It doesn't matter if you don't see it, let's take a look at it step by step.

 Code logic: First, A=43 is output directly from the innermost layer. Then, the return value of the innermost printf is output as 2 by the printf of the middle layer in the format of %d. Finally, the printf of the outermost layer outputs the return value of the middle layer in the format of %d 1.

In fact, observe the output value of the second printf and the output value of the third printf, the output value of the first printf and the output value of the second printf are not difficult to find: the return value of
printf is the number of characters output

The third printf outputs "43" and the number of characters is 2, so the return value is 2, and the second printf outputs "2". The second printf outputs "2" and the
number of characters is 1, so the return value is 1. The first printf will output "1"

One thing to note: the return value is the number of characters output, including numbers, letters, punctuation marks, spaces, etc.

printf("%d\n", printf("0,1,2,3\n"));

It is not difficult to see from the running results that the number 0123 occupies one character respectively, the punctuation marks " , " also occupy one character position respectively, and the newline character " \n " also occupies one character position.

If you add a sentence to the code, guess what the value of num is?

int num=printf("%d\n", printf("0,1,2,3\n"));
printf("%d", num);

The answer is 2, and the value of num is 2, which means that the return value of num accepting printf is 2, and it also shows that the number of characters output by printf is 2, one of which is the number 8, and the other is the "\n" in printf format control. Visible, format control The characters in are also included in the return value!

Then someone asked again? Why is the result not 9 for the first time?

Because it outputs 8 first, and then wraps. Pay attention to the order of execution! ! !


5.Common problems in printf

  • When there are format characters guided by % in the format control string, the quantity and type in the output value parameter list must be consistent with the format characters

analyze:

  1. The output parameter b in the first printf function is a double value 58.8, but the corresponding format control character is %d. When the types are inconsistent, no type conversion will be performed, and the actually transferred double value will be regarded as required. The integer type is understood, so unexpected results appear;
  2. In the second printf function, the format control string gives two %-guided format characters, but there is only one parameter a in the output parameter table, and the parameter is missing. Therefore, the value of the output c defaults to the data value of the storage unit behind the variable a in the memory, and the value of c cannot be determined .
  • When there is no format character guided by % in the format control string, there is no need to output the value parameter table, and the content of the string is output directly, and the escape character is output according to the actual meaning after escape, such as:

  • implement the left alignment method 
  • Use the "-" sign to control the left alignment, and you can ignore the field of view width control


6. Summary 

  1. Commonly used format controls are generally %d, %f, %c, %s, and their unique output methods
  2. Each output can set the field width, and the default content is right-aligned
  3. When there are format characters guided by % in the format control string, the quantity and type in the output value parameter list must be consistent with the format characters
  4. When there is no format character guided by % in the format control string, there is no need to output the value parameter list, and the string content is output directly, and the escaped characters are output according to the actual meaning after escaping
  5. Realize the left alignment method  : use the "-" sign to control the left alignment

Input and output of date of birth

Small exercises, can you check if you have mastered it?


7. Preface

This article introduces in detail the concept, usage, precautions, and practical operation of the printf function in C language.

Due to limited space and limited personal ability, there may be incomplete content and errors, and I hope everyone can actively correct them. I believe this article can help Xiaobai to a certain extent, and it can also be used as a review for big brothers. Thank you for your support.

Guess you like

Origin blog.csdn.net/weixin_59511824/article/details/131414951