Headache data input, understand data input and output

Table of contents

1. Input data with scanf function

1. The general form of printf function and scanf function

2. Format characters

2. Problems that should be paid attention to when using the scanf function    

3. Character input and output functions

1. Use putchar to output characters

2. Input characters with getchar function


When we use input and output functions, there will always be some inexplicable problems

Something like this doesn't match what we want to output 

Let's learn more about input and output functions today

1. Input data with scanf function

1. The general form of printf function and scanf function

printf (format control, output list)

scanf (format control, address list)

Format control: It is a string enclosed in a double quote (" "), called a format control string , which contains two pieces of information

  • Format statement: It is composed of "%" and format characters, for example: %d, %f. Its function is to convert the output data into the specified format and output it
  • Ordinary characters:

Address list:: A list consisting of several addresses, which can be the address of a variable or the address of the first character of a string.

2. Format characters

format character illustrate
d Input (output) a signed decimal number
u Input (output) unsigned decimal number
o Input (output) unsigned octal numbers
x  X Input (output) unsigned hexadecimal number
c input (output) a single character
s input (output) string
f Input (output) single-precision floating-point numbers
lf Input (output) double-precision floating-point numbers
domain width

Specify the width (number of columns) occupied by the input data, and the field width is a positive integer 

Example: %7.2f 7 means that the data width is 7

                    .2 indicates that the number retains two decimal places    

2. Problems that should be paid attention to when using the scanf function    

1. The format control in the scanf function is followed by the variable address , not the variable name.

As shown in the picture: this way of writing is wrong, you need to add "&" 

 2. If there are other characters in the format control string except the format declaration, the same characters should be input at the corresponding positions when inputting data. 

 3. When using the "%c" format to input characters, the characters in the space character and the "escape character" will be entered as valid characters

We found that the output of adding a space between two characters and not adding a space is different when inputting

When inserting a space between two characters, the system will send the first character 'a' to ch1; the second character is a space character ' ' to ch2; the third character 'b' to ch3, so that Print out the second case.

Note: When entering values, a space (or other delimiter) needs to be inserted between the two values ​​so that the system can distinguish between the two values. When continuously inputting characters, do not insert spaces or other separators between two characters (unless there are ordinary characters in the format string in the scanf function, at this time, insert characters at the original position when inputting), the system can distinguish between two characters .

4. When entering numerical data, if you enter a space, enter a carriage return, or encounter an illegal character (a character that does not belong to a numerical value), the data is considered to be over.

The %d format corresponding to the first data, the character 'a' is encountered after inputting 123, so the system thinks that there is no number after the value 123, and the first data ends here, so 123 is given to variable a and variable b It is in the format of %c; give 'a' to variable b, and the second data ends; the value after 'a' should be given to variable c, but '0' is wrongly typed as 'o', and the letter o is illegal character, which means that the numerical data is over here, and 123 is given to the variable c, and the following characters cannot be read.

3. Character input and output functions

1. Use putchar to output characters

 

We changed the code and found that the output is the same, why?

We know that the character type is also an integer type , so assigning a character to a character variable has exactly the same effect as assigning the ASCII value of the character to a character variable (but it should be noted that the range of integer data is 0-127) The .putchar () function is a function that outputs characters, it outputs characters but not integers . 66 is the ASCII value of character B, so putchar(66) outputs character B.

Explanation: c in putchar(c) can be a character constant, an integer constant, a character variable or an integer variable.

2. Input characters with getchar function

The getchar function can only receive one character, if you want to input multiple characters, you need to use multiple getchar

 Explanation: When inputting information with the keyboard, it is not that a character is typed on the keyboard, and the character is sent to the computer immediately. These characters are temporarily stored in the buffer of the keyboard, and only when Enter is pressed are these characters sent to the computer together , and then assigned to the corresponding variables in sequence.

What happens if you press Enter every time you type a character

We found that two lines of B and O were output respectively. What is the reason for this?

 In fact, what we input in the first line is not a character B, but two characters: B and a newline character , where B is assigned to variable a, and the newline character is assigned to variable b ; the second line enters two characters: O and newline , where O is assigned to variable c, and newline is not assigned to any variable . When outputting, the character B is output, then a newline, then the character O is output, and finally putchar('\n') is executed for a newline.

Note: The getchar function can not only obtain a displayable character from the input device, but also obtain characters that cannot be displayed on the screen, such as control characters.

I hope you can gain something from reading it.

Guess you like

Origin blog.csdn.net/2301_76207836/article/details/130079297