The scanf function and multiple sets of inputs in C language

1.Introduction to various types of data in scanf

%c——Input character (char)

%s——Input string

%d——Input integer type (int...)

%f——Input floating point type (float)

%lf——Input floating point type (double)

Note: %p - print address (incoming address &)

2.scanf’s return value and multiple sets of inputs

scanf is the input function

The return value of the function is:

1. If the reading is successful, the number of data actually read is returned .

2. If the reading fails, EOF is returned . The value of EOF is -1 .

So if you want to write a program with multiple sets of inputs, you can use a while loop and use the return value of scanf as the judgment condition of the loop.

The above multiple sets of inputs are of type int, so EOF can be used as a judgment condition.

But if it is a char type, can EOF be used? In fact, it is not possible. Let’s look at an example:

This question requires determining whether the input is a letter, which is obviously a multiple set of inputs of type char.

Let’s look at an example of an error

At first glance, it seems to be correct, but if you input multiple groups, will you use spaces or newlines to separate each input?

When entering a space or a newline, your scanf is also not equal to EOF. What should you do at this time?

We can replace !=EOF with ==1 , and that’s it!

 

Guess you like

Origin blog.csdn.net/m0_75186846/article/details/131223530