[Elementary C Language] A super detailed introduction to the scanf function and multiple sets of input

Thank you for visiting Ezi's works

 


When we learn a function, we need to focus on three points : 1. What is the function 2. The usage of the function 3. The details of attention

1. Re-recognize the scanf function

To know a function, we need to understand its function, return value, and received parameters in three aspects

function prototype

int scanf ( const char * format, ... );//函数原型

 (1) Function introduction

         The scanf function is an input function , so the received parameter type needs to meet a certain format

   The input formats are: various characters, such as strings, single characters and numbers, etc.

        Example 1: Input integer and character

#include<stdio.h>
int main()
{
	int a = 0;
	char ch = 0;//初始化

	scanf("%d %c",&a,&ch);//输入数据

	printf("%d\n", a);
	printf("%c\n", ch);//打印数据
	return 0;
}

The first line: 520 w is the data input by scanf; the second line: 520 w is the data printed by printf

       Example 2: input string

#include<stdio.h>
int main()
{
	char a[20] = { 0 };
	char b[20] = {0};
	int ret = scanf("%s %s",&b,&a);//最好有空格隔开
	printf("%d\n", ret);
	return 0;
}

It should also be noted that & means to take the address , and when inputting an array, since the array name is the first address, there is no need to add &. Of course, there is nothing wrong with adding it for fear of mistakes.

 (2) Return value type:

         Why int? Let's first look at the original text analysis:

 What is the number of successfully filled items? In fact, it is the number of successfully entered data (the number is shaping), we use code to analyze

Summary: The return value of the scanf function successfully receiving data is the number of received data (a format of %d (or %c, etc.) can only be counted as one number, which will be shown later in the article)

 Now that the return value of successfully input data is clear, what is the return value of failure?

      Pressing the keyboard three times in a row simulates the scenario of not inputting data. The scanf function does not receive data, and then the displayed return value is -1. In fact, scanf fails to receive data and returns EOF, and the value of EOF is -1 . So we think that the return value is -1 or EOF is okay

Summary: The return value EOF of scanf function failing to receive data (EOF is -1 by default)

In the above content, we have learned about the scanf function and some basic usages of it . The following is the focus of this section.


2. Other applications of scanf - multiple sets of input

Foreword: I believe you must have encountered such a question when you were brushing the questions, and there are such iconic words in it; multiple sets of input

like this:

 or something like this:

    When many students were doing it, they didn't consider what the meaning of multi-group input was, and just used a scanf function to solve the battle. Of course, I was like this at the time. This is a classic mistake, and the standard is zero.


Let's take the second question as an example:

  Topic: It is said that those with an IQ above 140 are called geniuses. KiKi wants to know whether he is a genius. Please help him program the judgment. Input an integer to represent a person's IQ. If it is greater than or equal to 140, it means that he is a genius, and output "Genius".

    Enter a description:

Multiple sets of input, each line of input includes an integer representation of the IQ.

   Output description:

For each line of input, output "Genius".

   Example 1

enter:

160

output:

Genius

Let's start with a wrong demonstration : the following code can only be judged once , what we need is to input several data and judge several times

#include<stdio.h>
int main()
{
	int a = 0;
	scanf("%d",&a);//错误写法,不符合题目要求
	if (a >= 140)
		printf("Genius");
	return 0;
}

You can judge several times by entering several times, so you should think of using the loop method at the first time. How many times does it cycle? This is non-deterministic, so we choose to use a while loop.

First improvement:

while (scanf("%d", &a))//套上while循环
{
    if (a >= 140)
		printf("Genius");
}

We know that using a while loop requires three steps : loop condition, loop body, and loop adjustment . What is the above-mentioned loop condition? We have learned the scanf function above and know its return value, so we can use the return value of the scanf function as the loop condition: read the data and return 1 successfully, and the loop continues; read the data Returns EOF (default -1) on failure, and the loop ends.

Second improvement:

while(scanf("%d",&a)!=EOF)//读取数据成功、循环继续
{
     if (a >= 140)
		printf("Genius");
}

After improvement, the complete code and running diagram are attached :

#include<stdio.h>
int main()
{
	int a = 0;
   //while(scanf("%d",&a)==1),循环条件这样写也可以
	while (scanf("%d", &a)!=EOF)
	{
		if (a >= 140)
			printf("Genius\n");
	}
	return 0;
}

Summary: In order to achieve multiple sets of inputs, use the return value of the scanf function as a loop condition

        We can see that three sets of data can be input and their results can be output, so how to end the loop? Of course, it should also be expressed according to its return value, so that the scanf function fails to receive data. You can do this in windows, press it three times in a row ). ctrl+z+Enter (Enter) , demonstrate again:

 

 Summary: To end the loop, press ctrl+c+Enter three times


 3. Supplementary details

Follow-up knowledge supplement:   

         Some students want to ask, why can a set of data enter several values? like this

Note 1: return value and input

      We need to know one more thing about the return value and input of the scanf function 

      Input: The sacnf function reads data from the buffer, not directly from the keyboard (introduced in the section where the getchar function clears the buffer), and the scanf function no longer reads data when it encounters a space.

      We enter 520 for the first time, then a space, and then enter 1314; the data read by the scanf function for the first time is 520, and then the judgment (return value 1) is established, print Genius, and then return to the judgment condition (need to read the data first ), so 1314 is read by the scanf function for the second time, and the Genius is printed when the condition is met.

     Return value: We use examples to introduce to you

 We can also print the value of a, the data read by scanf is only 520.

 So how can the scanf function return 2, 3...? Just add a few more %d (or %c, etc.)

 

 The upper limit of the return value of the scanf function is determined by the number of %d, and the return value of two %d can also be 1.

Note 2: spaces and newlines

In the input format, if there is a space between two %ds, it will not affect the input and output; but it is best not to add \n at the end of %d (scanf will automatically wrap the input data after carriage return), which will affect the input and output

   The original execution process is to print the data after entering 520 and press Enter, but the expected goal cannot be achieved if \n is added (the reason is temporarily stored)

Note 3: The third return type of scanf:

   We all know that using the sacnf function needs to follow a certain format, such as %d, which requires the input of integers; %c requires the input of characters

 Summary: When the input format does not match, the return value is 0

 Four. Summary

1. The scanf function is an input function, and the input data needs to follow a certain format

     Note; %d is the input integer data %c is the input character data %s is the input string......

2.scanf has three return values:

   (1) Return the number of data successfully read (2) Return EOF (default -1) if the read fails (3) Return 0 if the input format does not match

3. The scanf function can be used to implement multiple sets of input (with while loop and return value)


The content of this section is introduced here.

     If there are any mistakes in the above content, you are welcome to leave a message in the comment area; or if there are any details that have not been introduced, you are also welcome to add them, and I will make them up later. If there is something unclear, please understand

Note: The input format has not been introduced yet (subsequent supplement)

Guess you like

Origin blog.csdn.net/2301_77053417/article/details/131861314