Talking about several skills of scanf functions

zhaozj2021-02-11  232

Talk about several skills of scanf

"Hahaha, everyone, the kiss, today, I will tell some of the usage of the Scanf function", "What, this kid is bullying us, don't know!", "SCANF will not!" ..... A big pile egg, rotten tomato, throw it up, some people shout "this kid iron is not flat", "must be a deception fee!". . . . . . . . . . . (Author: Hasty wrong way to write this money to be made there, I have poor blood selling fast, and woo ........... "!)

-------------------------------------------------- ------------------------------

-------------------------------------------

Today, I will talk about three questions:

A .scanf function inputs the string in the format.

The scanf function input format can also contain a normal string, but his meaning is that these characters must appear in the input.

,E.g:

Int Num;

Scanf ("Hello% D", & Num);

His meaning is to first ask a Hello string, then enter a decimal number. Note when waiting for input

Ignore between Hello and the number to enter

Space, player, Enter. Therefore, these two inputs are correct:

Hello 1234

Hello1234

Two. The return value of the SCANF function.

See a school-written program:

#include

Main ()

{

Int Num;

Printf ("Please Input the Student's Score:");

Scanf ("% D", & num);

IF ((Num <0) || (NUM> 100))

{

Printf ("" The Score Put isnt Fine. Please run and input again. ");

}

Else IF (Num> 90)

{

Printf ("The Grade IS A.");

}

ELSE IF ((Num> 80) && (Num <90))

{

Printf (................

...........

}

............

}

This program is right, but if someone wants to make trouble, it is not an input number when entering, but what word is

So Congratulation,

This program crashes. (^ @ ^)

How to prevent this, some people receive the number to read into an array, and then determine whether reading is not a number

Word ........., the practice is really fun.

If you know the return value of the Scanf function, this problem is better. The SCANF function returns when the SCANF function is successful.

Value is the number of variables successfully read,

If the reading of the first variable is noted, the return value is 0.

Hahaha, we can stop the user's incorrect input by judging the return value executed by the Scanf function.

The process of controlling the program.

Change the above program:

#include

Main ()

{

INT NUM, int result = 0;

Printf ("Please Input the Student's Score:");

While (result == 0)

{

Fflush (stdin); / * Empty the input buffer. * /

IF (Result! = 1) Printf ("Please Input A Digital Score:");

Result = scanf ("% d", & num);

}

............................}

Everything is OK!

Application of a parameter in the SCANF function.

In the scanf function, we can use% c to read a character, read a string using% s. But read the word

When the string does not ignore spaces, ignore the start of the space when reading the string.

And read the space, so we can only read a word, not a string of strings. So it is generally read.

Take a string.

In fact, the scanf function can also complete such functions and is more powerful.

Here mainly introduces a parameter,% [], this parameter is to read a collection of characters. [] Is a collection of standards

Zhi, so% [] specifically refers to those characters defined by this collection, such as% [A-Z] is entered the uppercase letter, once you encounter

This collection of characters stop. If the first character of the collection is "^", this shows that the word is not in "^" back collection

The characters that are both "^" after "^" will stop. Note that the character string read at this time can contain spaces.

EG. Enter a string that only contains lowercase characters. It encounters the first time that is not lowercase characters.

Scanf ("% [A-Z], STR);

Eg. Want to enter a string, encounter "." Stop, design as follows:

Scanf ("% [^.]", STR);

Use this parameter, you can complete a lot of powerful features!

-------------------------------------------------- ------------------------------

----------------------------------------

Dear folk folks, this is why I am careful when I use the SCANF function ...........

(I think of the snoring of the thunder, "So simple things, you are imagi!", "Retreat ticket, return the ticket!", Sudden, author

It is buried by a big pile of flying smells ............)

The author: This is my first technical article, there will be some mistakes, welcome everyone. In fact, I prefer C , maybe I will write some C articles in the future, welcome the guidance of the master.

If you can or have your support, I will continue to write. (^ - ^)

Author: hyqryq

Again: Usually, the Scanf function and some of his parameters are not very common, mainly because:

1. Many system's scanf functions have a vulnerability. (Typical is that TC is then input to floating point models)

wrong).

2. Usage is complicated, it is easy to go wrong.

3. It will be difficult to analyze the compiler to analyze, which affects the quality and execution efficiency of the target code.

转载请注明原文地址:https://www.9cbs.com/read-5712.html

New Post(0)