A little harvest at the beginning of UNIX C

zhaozj2021-02-16  53

Below is a small program I have encountered when I have written the code, the problem is not big, but I am busy for a long time! Below I use the relatively good way to give this experience to everyone.

Please write this code under the VI editor.

/*a.c*/

#include

Main ()

{

INT i = 0;

While (i! = 1)

{

Scanf ("% D", & i);

}

}

I have asked a few friends: there is no problem with this code; all say: There should be no problem! Then let's test it.

Let's write a simple Makefile first

Below Enter Under UNIX

$ Vi Makefile

Edit the following code under the VI editor

# This is a parakefile code

# 作 作: Compile A.c into executable file a

Headers = incrude / stdio.h

Sources = a.c

Product = a

CC = CC

$ (Product): $ (SOURCE)

$ (CC) -O $ (Product) $ (Sourcees)

After the editing is complete, let's perform makefile

$ Make

Cc -o a a.c

Now an executable A has been generated, and below will start writing our unit test plan

Test Number Input Output Test Results Analysis 1 1 Jump

2 2 Go to the next input status

3 1.2 Jumping out

4 1.9 Jumping out

5 0.9 Go to the next input state

6 A Go to the next input status

$ a

1

$

...

...

...

Fill in the test schedule while testing

Test Number Input Output Test Results Analysis 1 1 Jump

2 2 Enter the next input state correct 3 1.2 Jump out of the correct 4 1.9 Jump out of the correct 5 0.9 Go to the next input state correct 6 A Go to the next input status

It's okay to see if there is no problem, there is another step to complete, enter a non-numerical, A, Enter! It seems that there is no problem! Enter 1 Enter! ! A problem occurred! Did not quit the loop! Ctrl C exits!

$ a.out

a

1

^ C $

Test Number Input Output Test Results Analysis 1 1 Jumping out of this code There is a serious error will not be released 2 2 Go to the next input state correct 3 1.2 Jump out 4 1.9 Jump out of the correct 5 0.9 Go to the next input state correct 6 A Go to the next input state error

Why is this so? A serious bug has appeared immediately. First let us add a sentence behind the Scanf statement:

Printf ("% d", i), look at what changes happened later, then repeat the number 6 test step, you will find that the program is executed into the dead cycle! It is in a non-stop print 0! In other words, the Scanf is not executed at all! Why is this so? Where is our scanf going? After some serious study and listening to the teacher's guidance finally understand, it turned out to be like this. First look at the concept below.

SCANF returns to EOF, and SCANF returns immediately if it reads an incorrect character (eg, the letter should be a number), it also returns immediately. Scanf returns the number of conversions it complete; if it does not complete any conversion, it returns EOF. SCANF stops when reading a commission. Unless the user clears the wrap, it will remain in the buffer of the standard input device. If the programmer does not clear the input buffer before the second call SCANF, the result is generated.

Below we are experimenting, rewrite the code below the code.

#include

Main ()

{

INT i = 0;

INT j = 0;

Char buffer [256];

While (i! = 1)

{

FGETS (Buffer, 256, stdin);

Printf ("****** / n% s / n ******** / n", buffer);

Fflush (stdin);

J = scanf ("% d", & i);

Printf ("% d / n", j);

}

Compile and execute, first enter any value, such as A; this time the input buffer result is A; then enter a number, such as 2, this time SCANF will return 1, so that the input conversion is successful, input The buffer will be cleaned, so there is no value output.

Next, let's try to enter a A, enter, this time scanf will return a 0 to show an error in the input conversion, and the A and Enter will be printed in the buffer, whereby the next If there is no buffer If FFLUSH is conducted, Scanf will accept this A and Enter of the input buffer, so that you enter the dead cycle!

That is to say, the input we have just conducted, when entering the letter and then carries back, Scanf returns an EOF, and this result will remain in the buffer, when the next program is called SCANF, due to this input buffer memory Error, scanf will not be executed correctly, so that the value of i cannot change, so that the program has become a dead cycle!

The problem found, the method of solving is to clear the buffer before calling Scanf, ie, in front of it:

Fflush (stdin);

/*a.c*/

#include

Main ()

{

INT i = 0;

While (i! = 1)

{

Fflush (stdin);

Scanf ("% D", & i);

}

}

Test this code once again! What should I do next to it!

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

New Post(0)