C language basics - the return value of the scanf function and its application

Preface, the scanf function has a return value

When we are doing programming tasks on PTA, we often see the following information in the compilation window:

warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]

At that time, the teacher would definitely tell you that this is a "warning" alarm and you can ignore it, which is indeed the case. However, we can at least know one thing about this alarm message, that is, there is a return value after the scanf function is called. Next, we will discuss the scanf return value in detail. And give some functions that can be achieved by using the return value of scanf during programming.

1. Return value of scanf

Let's look at an example first.

enter:

3 5

Code:

#include <stdio.h>
int main () {
	int a,b,c;
	c = scanf("%d%d", &a, &b);
	printf("the return value of scanf is: %d\n", c);
	return 0;
}

Output:

the return value of scanf is: 2

From this example we can see that the scanf function does have a return value. When we execute the above code and input the two numbers 3 and 5, the scanf function will be called first, and the two integers will be input (scanf will have a return value after the output is completed), and then the printf function will be executed, and the printf function will be executed according to the format. The return value of scanf is output. From this example, we can also boldly guess that the return value of the scanf function is the number of variables correctly read by scanf. This guess is half correct, the complete statement should be:

The return r of the scanf function satisfies: (1) When there is input in the input stream, r is equal to the number of variables correctly read by the scanf function; (2) When there is no input in the input stream, r is equal to -1.

Note: No input and incorrect input are different situations. Let’s give an example below.

Code:

#include <stdio.h>
int main () {
	int a,b;
	printf("%d\n", scanf("%d,%d", &a,&b));
	return 0;
}

For the above code, we give three input and output examples to illustrate.

Output 1:

3 5

Output 1:

1

Note that the first parameter of the scanf function call in the code, that is, the format matching string is "%d,%d", and the input in the output stream is "3 5". Obviously, the scanf function can correctly input the '3' in the input stream; then it matches the string according to the format. After 3, it should match a ',', but there is no correct input in the input stream. matches, so scanf cannot continue input and returns. Because there is already a correct input, the return value is 1.

Output 2:

,3,5

Output 2:

0

In this example, the first (non-blank) character ',' in the input stream does not match the first character '%d' of the matching string, so the scanf function Unable to continue typing and return. There was no correct input at this time, so the return value is 0.

Output 3:

^Z

Output 3:

-1

Note that the input in input 3 is not "^Z", but is simulated by using ctrl+z under Windows without any return value. Using ctrl+d under Mac, the input is empty on the PTA platform. At this time, there is no input in the input stream at all, so the return value is -1.

2. The wonderful use of the return value of the scanf function

We often encounter such input problems in PTA jobs, that is, you need to enter some data, but we do not tell you how many data there are in total. At this time, we can use the return value of scanf to solve this problem.

To give a specific example: it is required to input a set of integers, and input the odd and even numbers of this set of integers in the input order, input 1 for odd numbers, and output 0 for even numbers.

Input example:

12 23 34 45 56 67 78 89 91 13 24 35

Output sample:

0 1 0 1 0 1 0 1 1 1 0 1

Code:

#include <stdio.h>
int main () {
	int a;
	while(scanf("%d", &a) != EOF) {
		printf("%d ", a%2);
	}
	return 0;
}

In the above code, we used a while loop combined with the return value of the scanf function to complete the input of unknown data. Specifically: the loop condition of while is scanf("%d", &a) != EOF. In this case, EOF will wait for -1. You can also write it directly as -1. When there is still input in the input stream, the return value of scanf("%d", &a) cannot be equal to -1, the loop condition is true, and the loop continues. When there is nothing to input at the end of the input stream scanned by scanf, scanf("%d", &a) returns -1, the loop condition is false, and the loop ends.

Note: If you enter several characters separated by spaces, you need to match spaces at appropriate positions in the matching string.

Guess you like

Origin blog.csdn.net/morn_l/article/details/134390611