C Input Function details

Source: Internet
Author: User

1. After the input function is called, it first checks whether the number of characters in the input buffer has met the input requirements. If yes, it will be read directly from the buffer; if the number of characters in the buffer does not meet the input requirements, wait for the user to enter. The input is usually completed by the user entering the carriage return \ n, then the input function will repeat the previous step-check whether the number of characters in the input buffer meets the input requirements, if yes... if not...

Example:

#include<stdio.h>void main(void){int v1,v2;scanf("%d%d",&v1,&v2);        printf("scanf end\n");        return;}

Gcc-o t test. c

./T

1 // The program enters the waiting for input status. We enter 1 and press Enter.
// The program is still waiting for input, because only one "1" in the buffer is detected, and scanf requires two integers. Enter here
1 // The program is still waiting for input. We enter an integer 1 and press Enter.
Scanf end // the scanf function completes reading and returns
Shell returned 2

However, the getc () function does not use \ n as the input completion mark. As long as the user inputs, it starts processing immediately and returns the result.

2. All Input Functions share an input buffer.

Example:

#include<stdio.h>void main(void){int v1,v2;scanf("%d%d",&v1,&v2);printf("%d",getchar());return;}

Gcc-o t test. c

./T

1 2
10 // output the Enter key ascii code, 10

./T

1 2 3

32 // The "3" ascii code is not output here, because "2" is followed by spaces, while getchar () does not skip spaces.

Shell returned 2

It is also seen that the carriage return is a special input, which sends a "user input completed" notification to the input function, and also leaves a "\ n" character in the input buffer.

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.