Use (while (getchar ()! = '\ N');) Remove the carriage return

Source: Internet
Author: User

Just a few simple, if there are deficiencies, welcome to shoot bricks ....

I am still studying the basics of C. Today I see the example program in the book using while (getchar ()! = '\ N'); To remove the carriage return (specifically, the line break should be removed ).

I didn't understand it at the beginning. Why can this statement remove line breaks? What is the detailed process? In some cases, why can all illegal input before the carriage return be cleared?

After searching for previous questions raised by our predecessors and reading the function annotations of the getchar () function, we gradually understood the while (getchar ()! = '\ N.

The example program in the book is described as follows:

# Include <stdio. h> # define INPUT_LINE_LENGTH40int main (void) {FILE * outputFile = NULL; int quitChar; char inputText [INPUT_LINE_LENGTH]; outputFile = fopen ("outfile.txt", "w "); if (outputFile) {for (quitChar = 'n'; (quitChar! = 'Y' & quitChar! = 'Y');) {printf ("Enter a string of no more than 40 characters \ n"); printf (">>>"); gets (inputText ); fputs (inputText, outputFile); fputc ('\ n', outputFile); printf ("\ nQuit? (Y/N): "); quitChar = getchar (); // The while (getchar () in this example ()! = '\ N'); it is a type that can eliminate illegal input while (getchar ()! = '\ N'); putchar (' \ n');} fclose (outputFile);} return 0 ;}

There is nothing to say in other places, which is very simple. The following two sentences are introduced:

quitChar = getchar();while(getchar() != '\n');

To find out which operations are available, you should first understand the behavior of the getchar () function. The following is an introduction to Baidu Baike:

Getchar ():

When the program calls getchar, the program waits for the user to press the key. The characters entered by the user are stored in the keyboard buffer until the user presses enter (the carriage return character is also placed in the buffer ).
After you press enter, getchar starts to read one character each time from the stdin stream.
The Return Value of the getchar function is the ASCII code of the first character entered by the user. If an error occurs,-1 is returned and the characters entered by the user are displayed on the screen.
If you enter more than one character before pressing enter, other characters will be kept in the keyboard cache, waiting for the subsequent getchar call to read. That is to say:
Subsequent getchar calls do not wait for the user to press the key, but directly read the characters in the buffer until the characters in the buffer are read as before waiting for the user to press the key.

Now, let's start to formally explain the above two statements.

quitChar = getchar();

For ease of interpretation, assume that the input is NYYN '\ n' (representing NYYN "Press ENTER ")

This statement uses the getchar () function. Therefore, the value of quitChar is assigned as the return value of the getchar function, which is the ASCII code of the first character entered by the user, that is, 78.

The program continues to run down and starts to run

while(getchar() != '\n');

Because the Keyboard Buffer also contains the input yyn' \ n' (n has been removed), The while LOOP continues to use the getchar () function to read the remaining characters in the Keyboard Buffer one by one.

Finally, when getchar () Encounters '\ n', The while () loop condition is not true and the loop ends.

In this way, the invalid YYN and the last '\ n' are consumed by this statement and the task is completed.

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.