The characteristics of GetChar (), gets () and scanf () in C language and the problem of excess carriage return caused by scanf

Source: Internet
Author: User
Tags function prototype

The first thing we recommend is to use the gets function less.

I encountered this problem is a C language novice more likely to encounter a problem. It's about get () and GetChar () two functions. As we all know, the gets () function is an input string, and GetChar () is a single character. But in a small program, I found two of them in some other usage.

because the code for the source file is very long, it only hits a function in the source file that involves the problem:

void input (Num,name)
{
   int num[];
    Char name[10][8];
    int i;
    
    for (i=0;i<n;i++)
    {
        printf ("\ n input number:");
        scanf ("%d", &num[i]);
        printf ("\ n intput name");
        GetChar ();
        Gets (Name[i]);
    }


The function is to enter the student number and name of 10 students.

at first, I think it is superfluous to add a getchar function before the gets function. So I deleted the beginning of the code, but the result is not run.

always skip the input name[i] after the num[i data has been lost.

Baffled , I had to search the data.

Finally I found the answer to the question.

with the scanf function, after entering the characters, click Enter. The characters that were originally entered are stored in the buffer and are read to the variable when the ENTER key is pressed. But after pressing the carriage return is redundant a carriage return character, GetChar () The function is to take this carriage return character to read out, otherwise was read by the gets () function, directly skipped the next variable's input. (Gets () can also be used here)

so the question arises again, why two scanf functions can be used continuously.

This is the difference between the gets function and the scanf function:

the scanf function cannot accept spaces, tab tabs, carriage returns, etc. when entering integer data;

The gets function can accept spaces, tab tabs, and carriage returns.

This is why you can use the SCANF function continuously.

In addition, the two have different points:

scanf function If a space (SPACEBAR and TAB) is entered, the string is considered to end, and the character after the space is processed as the next entry, but the gets () function receives the entire string of input until it encounters a change of behavior .

There is also a problem here, not after all the scanf are added GetChar (). Because we produce a carriage return character, it only affects the use of scanf input characters, but also in the input characters must be absorbed before the return character.

Finally, add the respective return values of these three functions:

1.scanf function

The return value of the scanf function can be divided into three different cases
1) A positive integer that represents the number of correct input parameters. For example, execute scanf ("%d%d", &a, &b);
If the user enters "3 4", can enter correctly, return 2 (the correct input two variables);
If the user enters "3,4", you can enter a correctly, cannot enter B, and return 1 (a variable is entered correctly).
2) 0, indicating that the user's input does not match and that no value can be entered correctly. As in the previous example, if the user enters ", 3 4", returns 0.
3 EOF, which is the constant (usually-1) defined inside the stdio.h, indicates that the input stream has ended.


2.gets function

Function prototype: Char *gets (char *string)

format: Gets (character array or pointer);

Return value of the gets function:
1.) normal read (no end of file encountered) (note, read into the empty string is also normal read), then return the string, that is, return the first address of the string, char* type.
2.) The end of the file is encountered and Null is returned.


3.getchar function

its return value is obvious.

char A;

A=getchar ();

is one of the characters we entered from the keyboard.


The article wrote here, I feel I am very necessary to see how the C language of the various standard headers in the definition of these functions, the help must be very large, to step up the start.

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.