For the supplementary content of this chapter, see 14 Formatted Input and Output Supplement
1. printf function
1.1. Format:
printf ( 格式串,表达式1 , 表达式2 ) ;
Format string = conversion description + ordinary characters
The number of conversion descriptions and the number of output items must be equal and correspond one-to-one, including the correspondence of variable typesprintf("%d %d", a,b); //正确 printf("%d %d", a); //错误! printf("%d", a,b); //错误!
1.2. Conversion instructions (including formatting information)
Format: For example: 、`%3.4f
% m.p X
%3d
m - the minimum column width . Indicates the minimum number of characters to display. If the actual number of digits of the data is greater than m, it will be displayed completely; if it is less than that, it will be right-aligned and the left space will be filled. If m is negative, align left . For floating-point types, m also includes decimal places and decimal places.
- Example:
the original data is 1234, the output of %3d is 1234, and the output of %5d is·1234
(aligned right, filled with spaces on the left)
p - precision. Depends on conversion type X
- X is d (decimal integer). p specifies the minimum number ,If it is insufficient, the number will be filled with zeros on the left. Without p, it defaults to 1.
- X is e (exponent, scientific notation). p indicates the number of digits that should appear after the decimal point, and if the decimal point is insufficient, 0 will be added at the end , and the default is 6 digits. p=0, no decimal point is displayed.
- X is f (fixed-point decimal) and p represents the number of decimal places.
- X is g (exponential form + fixed-point decimal floating-point number) p represents the maximum number of significant digits (not digits after the decimal point) of the exponent . No trailing 0's are displayed, and no decimal point if there are no decimals.
Summarize: m specifies the minimum column width, p specifies the maximum number of decimal places for floating point types, and the minimum number of digits for integer types (slightly different from m).
Example:
int i;
float x;
i = 40; x = 839.21f;
printf("|%d|\n",i); //加“|”便于观察输出长度
printf("|%5d|\n",i);
printf("|%-5d|\n",i);
printf("|%5.3d|\n",i);
printf("|%10.3f|\n\n",x);
printf("|%10.3e|\n",x); //注意这个
printf("|%-10g|\n",x);
The output is:
1.3, escape sequence
1.3.1 Symbol escape sequences
Example:
\n - new line
\a - bell bell
\b - back one bit
\t - horizontal tab, move the cursor to the position of the next tab character. The width of the horizontal tab is determined by the operating system, usually 8 characters wide
\"——represents the character ""
", ""
"marks the beginning and end of the string
\\——displays a\
Program example 1:
printf("Item\tUnit\cPurchuse\n\tPrice\tDate\n");
printf("\"Hello\"");
The output is:
1.3.2 Numeric escape sequences
Symbolic escape sequences can be used by replacing the corresponding ASCII code (octal or hexadecimal) with numeric escape sequences. For example: \n
== \12
== \x0a
==\x0A
See the ASCII code table for details.
Two, scanf function
1. Format:
scanf
The function format printf
is the same as function.
Example:
int i,j;
float x,y;
scanf("%d%d%f%f",&i,&j,&x,&y); //scanf函数转换说明紧挨着的情况很普遍,printf则少有
illustrate:
- There should be an equal and one-to-one correspondence between conversion instructions and variables
- The variable type also needs to correspond
&
The symbol cannot be omitted, otherwise the warning " format argument is not pointer " may be generated- Reading data using
scanf
functions is simple and effective but not ideal, you can read it in character format and convert it to numeric form. - When a function is passed a pointer:
int i, *p; //定义指针变量
p = &i; //指针p存储i的地址
scanf("%d", p); //传入p,无需&符号
↑↓ The upper and lower codes are equivalent ↑↓
int i;
scanf("%d", &i);
2. Working method
(1)read process
- The conversion specification looks for an item of the appropriate type in the input data, stops when it encounters a character that does not fit this item, and puts it back in its place.
- Whitespace characters (including spaces, horizontal/vertical tabs, formfeeds, and newlines) are ignored when finding the beginning of a number
(2) Identification rules
A. For integers:
- First look for the positive and negative signs;
- Look for numbers again;
- Stop when encountering a non-number.
B. For floating-point types (%f, %e, and %g are the same):
- Look for sign;
- Find numbers (may contain decimal points);
- Look for the exponent (consisting of the letter e/E, optional sign and number).
Example:
The call statement for the input data
1-20.3-4.0e3
is:scanf("%d%d%f%f",&i,&j,&x,&y);
The input result is:
i = 1; j = -20; x = 0.3; y = -4000;
Detailed call process:
- Conversion specification %d. The first non-null input character is 1; since integers can start with 1, the scanf function goes on to read the next character, which is -. The scanf function recognizes that the character - cannot appear in an integer, so 1 is stored in the variable i, and the character - is put back.
- Conversion specification %d. Subsequently, the scanf function reads the characters -, 2, 0, and . (period). Because integers cannot contain a decimal point, the scanf function stores -20 in the variable j, and puts the character . back.
- Conversion specification %f. Next the scanf function reads the characters ., 3 and -. Because floating-point numbers cannot have a negative sign after the number, the scanf function stores 0.3 in the variable x and replaces the character -.
- Conversion specification %f. Finally, the scanf function reads the characters -, 4, ., 0, e, 3, and ¤ (newline). Because floating-point numbers cannot contain newlines, the scanf function stores [illustration] in the variable y and puts the newlines back.
- In this example, the scanf function matches each conversion specification in the format string with an input item. Because the newline is not read, it will be left for the next scanf function call.
3. Ordinary characters in the format string
blank character
scanf
The number of whitespace characters in the format string in the function does not matter, i.e.
//以下两条语句等价
scanf("%d %d %f%f");
scanf("%d%d%f%f");
- A blank character in the format string matches any number of blank characters (including 0) in the input
scanf("%d /%d",&num,&denum); //读取分数,输入的分号"/"前后可以有任意数量的空格,同样匹配
scanf("%d/%d",&num,&denum); //读取分数,输入的分号"/"前面不能紧邻空格,否则不匹配
Other characters (difficulty)
scanf
If other characters are encountered in the function format string, it will be compared with the input characters. If it matches, it will continue. If it does not match, it will exit abnormally.
Example:
- If the format string is
"%d/%d"
, the input is·5/·96
(• means a space), then match.- If the input is
·5·/·96
, the in the format string/
does not match the second space in the input, and thisscanf
function exits abnormally.%d /%d"
(there is a space in the corresponding position of the format string) to·5·/·96
match the input of .
Note:
scanf
The function retrieves input data based on the format string, and the blank characters in the format string are directly ignored. The key is to figure out the sequence.
4. The difference between the printf function and the scanf function
- Both functions call data;
scanf
Function calls must have&
symbols,printf
functions cannot;scanf
Blank characters are usually skipped when the function looks for data, except for the conversion description, the format string does not require other characters (including blank characters);printf
The format string of a function usually\n
ends with ,scanf
otherwise, it will cause a program error;scanf
function allows reading in entire fractions.
三、Q&A
%i
%d
difference with
For
printf
functions, there is no difference. Forscanf
functions,%d
only decimal numbers%i
can be read, and octal, decimal, and hexadecimal can be read. For example,%i
treat056
(with prefix 0) as octal,56
treat as decimal, treat0x56
(with prefix 0x) as hexadecimal.
- How to display
%
symbols?
%
Add an extra symbol before the ones you want to display%
.printf("%%"); //显示1个% printf("%%%"); //显示2个%
- The user enters " " between two numbers
,
,scanf
how does the function handle it?
Example:
scanf("%d%d",&i,&j);
Input: 4,28
Result:scanf
The function returns after storing 4 ini
.,28
Leave it for the nextscanf
function call.