Scanf () function (below)

Source: Internet
Author: User
Scanf () function (below)

I have already expressed two points in the previous article. I would like to reiterate here: 1. This article only discusses the use of scanf () function control strings. All routines in this Article do not constitute programming suggestions. 2. Everything must be done by yourself. Different compilers on different platforms may have different results. All routines in this article are debugged in WIN-TC + Windows ME.

Iv. Common Errors and Countermeasures for using scanf () function control strings

Problem 1: The program is compiled successfully, but the running error prompt is as follows:
Scanf: floating point formats not linked
Abnormal Program termination

Example program of error:

# Include <stdio. h>
Int main (void)
{
Int I, J;
Float s [3] [3];

/* Here */

For (I = 0; I <3; I ++)
For (j = 0; j <3; j ++)
Scanf ("% f", & s [I] [j]);

For (I = 0; I <3; I ++)
For (j = 0; j <3; j ++)
Printf ("% f", s [I] [j]);
}

This is actually a question that has nothing to do with the topic of this article. It is also a question of the compiler that has nothing to do with scanf () functions.

The reason is clear: no floating point library is linked. In the early days, system memory resources were insufficient, and multi-dimensional floating point groups occupied a large amount of memory (one-dimensional floating point groups do not have this problem). Therefore, TC should try not to add unrelated parts during compilation, if a floating point converter is not found, this part is not installed in the executable program. Sometimes TC cannot correctly identify the actual need for Floating Point conversion, so the above error will occur.

Solution: tell TC to convert the input of the floating point number. Add the following statement to the program marked with/* here.

Method 1: float c;
Scanf ("% f", & c );

Method 2: float c, * t;

T = & c;
.....

That is to say, as long as the compiler has the clue of Floating Point conversion, TC will connect the floating point conversion. So generally, there will be floating point variables in larger programs, but this is not the case.

But the problem is not over yet. I have a saying "one-dimensional floating point groups do not have this problem" on it, so let's take a look at this:

# Include <stdio. h>
Int main (void)
{
Int I, j;
Float s [3] [3], * ptr;

Ptr = & s [0] [0];


For (I = 0; I <3; I ++)
For (j = 0; j <3; j ++)
Scanf ("% f", ptr + I * 3 + j );

For (I = 0; I <3; I ++)
For (j = 0; j <3; j ++)
Printf ("% 7.2f/n", s [I] [j]);
}

In this way, we can reduce the multi-dimensional floating point group to a one-dimensional floating point group for processing. After debugging, the program runs normally. This indicates that the TC compiler is only dealing with this "unlinked Floating Point Library" problem when dealing with multi-dimensional floating point groups (struct.

Question 2: Does the scanf () function correctly accept strings with spaces? For example, I love you!

# Include <stdio. h>
Int main ()
{
Char str [80];

Scanf ("% s", str );
Printf ("% s", str );

Return 0;
}

Input: I live you!
Output: I

When the scanf () function receives input data, it ends a data input in the following circumstances: (instead of ending the scanf function, the scanf function only has data in each data field and ends after pressing enter ).
① In case of space, "enter", and "Skip" keys.
② End with width.
③ Illegal input.

Therefore, the above program cannot achieve the expected purpose. scanf () scans the space after "I" and considers the assignment of str to an end, and ignores the "love you! ". Note that" love you! "It's still in the keyboard buffer zone. (what we see on the Internet here is" Keyboard Buffer Zone ", but actually it's not. It should be an stdin stream. I will not correct it here, but I will continue using the original statement ). Let's change the above program to verify it:

# Include <stdio. h>
Int main ()
{
Char str [80];
Char str1 [80];
Char str2 [80];

Scanf ("% s", str);/* enter: I love you! */
Printf ("% s", str );
Sleep (5);/* Wait 5 seconds to tell you where the program is running */
Scanf ("% s", str1);/* you do not need to enter the two statements, but scan the keyboard disk buffer */
Scanf ("% s", str2);/* you do not need to enter the two statements, but scan the keyboard disk buffer */
Printf ("/n % s", str1 );
Printf ("/n % s", str2 );
Return 0;
}

Input: I love you!
Output: I
Love
You!

Okay, the reason is clear. Can the scanf () function complete this task? The answer is yes! Do not forget that the scanf () function has a % [] format controller (if you are not familiar with % [], refer to the previous article). Please refer to the following program:

# Include "stdio. h"
Int main ()
{
Char string [50];

/* Scanf ("% s", string); cannot receive space characters */
Scanf ("% [^/n]", string );
Printf ("% s/n", string );
Return 0;
}

Question 3: residual information of the Keyboard Buffer

# Include <stdio. h>
Int main ()
{
Int;
Char C;

Do
{
Scanf ("% d", & );
Scanf ("% C", & C );
Printf ("A = % d c = % C/N", a, c );
/* Printf ("c = % d/N", c );*/
} While (C! = 'N ');
}

Scanf ("% C", & C); why cannot this sentence normally receive characters? We use printf ("c = % d/N", c); Use int to represent C and enable printf ("c = % d/N", C ); in this sentence, let's see what the scanf () function assigns to C. The result is c = 10 and the ASCII value is 10? Line feed:/n. by the way, each time we press the "enter" key, we send a "Press ENTER" (/R) and a "line feed" (/N) to the keyboard buffer ), here/R is processed by the scanf () function (let's just move the pointer to the Keyboard Buffer Queue), And/N is scanf () the function is "incorrectly" assigned to C.

Solution: Add fflush (stdin) after two scanf () functions, and getch (); getchar () when the statement is added, we will not analyze it here. Let's explore it by yourself. But add fflush (stdin); whatever the situation is feasible.

Function Name: fflush
Function: clears a stream.
Usage: int fflush (FILE * stream );

# Include <stdio. h>
Int main ()
{
Int;
Char c;

Do
{
Scanf ("% d", & );
Fflush (stdin );
Scanf ("% c", & c );
Fflush (stdin );
Printf ("a = % d c = % c/n", a, c );

} While (c! = 'N ');
}

Here is an example of using a space character to process the residual information in the buffer zone:

Run the program with an error:

# Include <stdio. h>
Int main ()
{
Int I;
Char j;
For (I = 0; I <10; I ++)
{
Scanf ("% c", & j);/* There is no space before % */
}
}

After the space control character is used:

# Include <stdio. h>
Int main ()
{
Int I;
Char j;
For (I = 0; I <10; I ++)
{
Scanf ("% c", & j);/* Note that there is a space before % */
}
}

You can run it to see how different the two programs are.

Question 4 how to deal with the program deadlock or error caused by incorrect input of the scanf () function?

# Include <stdio. h>
Int main ()
{
Int a, B, c;/* calculate a + B */

Scanf ("% d, % d", & a, & B );
C = a + B;
Printf ("% d + % d = % d", a, B, c );
}

If the value of a and B is entered correctly in the above program, there is no problem. However, you cannot ensure that the user can enter the correct value every time. Once the wrong type is entered, your program is not a deadlock, but an incorrect result. Haha, maybe everyone has encountered a problem, right?

Solution: When the scanf () function is successfully executed, the return value is the number of successfully read variables. That is to say, your scanf () function has several variables. If all the scanf () functions are read normally, it returns a few. But pay attention to another problem. If illegal data is input, there may be residual information in the keyboard buffer.

Correct routine:

# Include <stdio. h>
Int main ()
{
Int a, B, c;/* calculate a + B */

While (scanf ("% d, % d", & a, & B )! = 2) fflush (stdin );
C = a + B;
Printf ("% d + % d = % d", a, B, c );
}

Let's end this article. I am lazy here to serve as a guest. it is very serious to encourage the finger to initiate a strike. It seems that the strike will not be able to let down without a heavy salary increase. (Hey hey, boss Jing, how can I help you ).

Gossip is idle, and I have to say a few modest words in my case. My level is limited (actually limited ^ _ ^, Which is the truth). The mistake is inevitable, thank you later.
(Full text)

Knocker 2004.10.21

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.