Advanced Application of SCANF Function in C Language

xiaoxiao2021-03-06  212

Maybe I am a little redundant, but I have seen many people asking this matter, I have to file this function.

In fact, in the C language, the most difficult is not a pointer, but the two functions of Scanf and Printf, here I only introduce advanced content.

The C language does not input the output statement. His input output is implemented by function, and the input and output stream is divided into three:

Stdio, Stdout, Stderr

In scanf ("% c") and scanf ("% c") (note the second space) This space is to shield / 0 because in our input system, the initial input data is on the keyboard buffer When he encounters / 0 (that is, the Enter key), the contents of the buffer and / 0 will be input to the STDIN stream. According to an example:

The following system simulates the bank zero exchange system

#include

Main ()

{

Char C;

Printf ("Please enter the zero currency you want to redeemed (the minimum amount of 5 cents per time exchange) / N") / n ")

Printf ("Do you still need to convert? (y / n)");

Scanf ("% C", & C);

While (c == 'y' || c == 'y')

{

IF (c == 'y' || c == 'y') Printf ("Do you need to convert? (Y / n)");

Scanf ("% c", & c); / * Note that there must be space * /

}

Printf ("Thank you for your use")

}

Suppose now is now YYY / N

The analysis is as follows: The first is the first SCANF to receive the first Y, and then enter the cyclic output printf ("Switch, do you need to exchange? (Y / n)"), at this time, SCANF function in the loop Receive data, due to space, work is only shielded / 0, and there is no, so the content is Y || Y, so it will be looped, and it will encounter / 0 after repeated output three times, and / 0 first by parameter space Shield, and% c failed to encounter an input character, so it will wait for the user's input.

Summary: Spaces are actually a parameter in the scanf function, and the scanf function and the printf function have a return value. The scanf function returns to the correct acceptable number of reference% D or% F, and the return of the PrintF is correct. The number of characters to the screen. If you do your specific understanding of the more advanced Scanf and Printf, and how C language is implemented from the underlying layer, please refer to the "C-language Practical Tutor", the Electronic Industry Press Foreign Computer Textbook Series, this book is a C language classic textbook, you can Said that there is currently no C language book to be compared to this book, query address

http://www.phei.com.cn/bookshop/bookinfo.asp?bookcode=tp69310 &booktype=main

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

New Post(0)