"C-language learning", C Primer Plus, chapter 8th character input/output and input confirmation

Source: Internet
Author: User

Learning Summary

1, the buffer is divided into full buffer (fully buffered) I/O and row buffer (line-buffered) I/O. For a fully buffered input, it is emptied when the buffer is full (the buffer content is sent to its destination). This type of buffer usually appears in the file input. For row buffer I/O, the buffer is emptied when a newline character is encountered, and the keyboard input is the standard row buffer.

2, EOF is a C to the end of the file an identity, defined in the stdio.h header file, #define EOF (-1). When using keyboard input, the EOF signal can be simulated via the ctrl+d:

#include <stdio.h>int main () {        int ch;        while ((Ch=getchar ())!=eof) {                putchar (ch);        }        return 0;}

Abc[enter]

Abc

123[ctrl+d]123

3, > and < are redirect operators, must be executable program plus files, take the above procedure as an example:

Execution:./test > ABC

Input:

Abcdefg[enter]1234567[enter]

Execution: Cat ABC

Output:

Abcdefg

1234567

4. In addition to the above redirection operators and the >> operator, this operator allows you to append data to the end of an existing file. There are also pipeline operators (|), which are actually operators on UNIX and Linux.

Execution:./test >> ABC

Input:

Hijklmn[enter]

Execution: Cat ABC

Output:

Abcdefg

1234567

Hijklmn

Execution:./test | grep AAA

Input: Aaabbbccc[enter]dddeeefff[enter]ggghhhaaa[enter]

Output:

Aaabbbccc

Ggghhhaaa

5, in creating a program with the user, you need to take into account all the boundary problems, such as the program only requires the user to enter a, B, C, D, in case the user input of other and a large number of characters will be handled, and so on. There are also programs that require getchar for character input and digital input using scanf, each of which can do its job well, but they do not mix well, because GetChar reads each character, including spaces, tabs, and line breaks, scanf, while reading a number, skips spaces, tabs, and line breaks.

6. Programming questions (question 8)

 1 #include <stdio.h> 2 3 int getchoice (void); 4 int getfirst (void); 5 6 int Main () {7 int ch,t,y; 8 float B; 9 int c;10 char sa[10],sb[10];11 ch =getchoice (), if (ch==113) {0;15}else{16 printf ("Enter First Nu Mber: "); scanf ("%s ", SA), while (SSCANF (SA,"%f ", &a)!=1) {p rintf ("%s is not a number.\nplease enter a number.such as 2.5, -1.78E8, or 3:", SA); scanf ("%s",                 SA);}22 printf ("Enter second Number:"), scanf ("%s", SB); 24 while (SSCANF (SB, "%f", &b)!=1| | (ch==100&&b==0))  {if (ch==100&&b==0) {-printf ("Enter a number other than 0: ");}else{28 printf ("%s is not a number.\nplease enter a num Ber.such As 2.5, -1.78E8, or 3: ", SB);}30 scanf ("%s ", SB); 31}32                 }33 if (ch==97) {("%s+%s=%.1f\n", sa,sb,a+b),}else if (ch==98) {37 printf ("%s-%s=%.1f\n", sa,sb,a-b),}else if (ch==99) {("%s*%s=%s\n", sa,sb,a*b); }else{41 printf ("%s/%s=%.1f\n", sa,sb,a/b),}43, return 0;45}46 int GetC          Hoice (void) {$ int ch;49 printf ("Enter The operation of your choice:\n"), and printf ("A. Add B. subtract\n "); printf (" C. Multiply d.divide\n "); printf (" Q. quit\n "); while (ch!=97&&ch!=98&&ch!=99&&ch!=100&&ch!=113) {printf ("plea Se Enter a right choice:\n "); Ch=getfirst ();}58}61 + int GetFirst (void) { T ch;64 Ch=getchAR (); GetChar ()!=10 return 0;67 return ch;68} 

Operation Result:

Enter the operation of your choice:

A. Add B. Subtract

C. Multiply D.divide

Q. Quit

F

Please enter a right choice:

D

Enter First Number:QQQ

QQQ is a number.

Please enter a number.such as 2.5, -1.78E8, or 3:1

Enter Second number:0

Enter a number other than 0:1

1/1=1.0

"C-language learning", C Primer Plus, chapter 8th character input/output and input confirmation

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.