Getting started with iOS development☞C language (buffer issue), ios Buffer

Source: Internet
Author: User

Getting started with iOS development☞C language (buffer issue), ios Buffer
Buffer: input buffer

Keyboard-Keyboard Buffer-input buffer-Program (scanf function)

Anything we input on the keyboard will first enter the input buffer area, and then the program will read the data one by one from the input buffer area.

 

Input buffer management method:

The number to be read first (the previous number is read in the input buffer before the subsequent number can be read)

 

After reading a data record, clear the input buffer to ensure that the previous input has an error and will not affect the subsequent input.

Scanf ("% * [^ \ n]");

Scanf ("% * c ");

 

The input buffer has the following features: this data is cleared only when the data is read.

When the data types in the scanf and the buffer are inconsistent, the data cannot be read, which may lead to an endless loop.

Solution:

Scanf ("% * [^ \ n]"); // * ignore any non-\ n characters in the read content; read all characters before \ n

Scanf ("% * c"); // read a character from the buffer and ignore it.

Supplement: scanf fails to call (main cause: Type mismatch) and returns the spam value.

Char ch;

// The following two rows can empty the content of one row in the buffer.

Scanf ("% * [^ \ n]"); // clear all content before the line break

Scanf ("% c", & ch); // read \ n from the input buffer

 

How scanf works
  1. Scanf starts input if it finds that there is nothing in the input buffer.
  2. Scanf finds \ n in the input buffer, then it will enter the buffer Value
  3. If you find your food is taken away and put it on your plate
  4. If it is not your own dish, you will go back on your own.

 

   

    

// Check whether the input format is correct

# Include <stdio. h>

Int main (){

Int num = 0; // Initialization is cleared

Printf ("enter a number :");

While (! Scanf ("% d", & num )){

Scanf ("% * [^ \ n]");

Scanf ("% * c ");

Printf ("incorrect input format! \ N enter a number :");

}

Printf ("the input is correct! Num = % d \ n ", num );

Return 0;

}

You need to clear the input buffer every time you use the scanf function to read data!

    

Output buffer:

Program (printf function)-> output buffer-> Screen

 

Conditions for program output to reach the screen

The condition that the output buffer content is displayed on the screen (at least one condition is met ):

  1. \ N
  2. Program end (function end)
  3. The output buffer is full.
  4. Use fflush (stdout) to force refresh (manual refresh)
Example:

 

 

Related Article

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.