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 intGetchoice (void);4 intGetFirst (void);5 6 intMain () {7 intch,t,y;8 floatb;9 intC;Ten Charsa[Ten],sb[Ten]; One ACh=Getchoice (); - if(ch==113){ - return 0; the}Else{ -printf"Enter First Number:"); -scanf"%s", SA); - while(SSCANF (SA,"%f", &a)! =1){ +printf"%s is a number.\nplease enter a number.such as 2.5, -1.78E8, or 3:", SA); -scanf"%s", SA); + } Aprintf"Enter Second Number:"); atscanf"%s", SB); - while(SSCANF (SB),"%f", &b)! =1|| (ch== -&&b==0)){ - if(ch== -&&b==0){ -printf"Enter A number other than 0:"); -}Else{ -printf"%s is a number.\nplease enter a number.such as 2.5, -1.78E8, or 3:", SB); in } -scanf"%s", SB); to } + } - the if(ch== the){ *printf"%s+%s=%.1f\n", sa,sb,a+b); $}Else if(ch==98){Panax Notoginsengprintf"%s-%s=%.1f\n", sa,sb,a-b); -}Else if(ch== About){ theprintf"%s*%s=%s\n", sa,sb,a*b); +}Else{ Aprintf"%s/%s=%.1f\n", sa,sb,a/b); the } + - return 0; $ } $ - intGetchoice (void){ - intch; theprintf"Enter The operation of your choice:\n"); -printf"A. Add B. subtract\n");Wuyiprintf"c. Multiply d.divide\n"); theprintf"Q. quit\n"); -Ch=GetFirst (); Wu while(ch!= the&&ch!=98&&ch!= About&&ch!= -&&ch!=113){ -printf"Please enter a right choice:\n"); AboutCh=GetFirst (); $ } - - - } A + intGetFirst (void){ the intch; -Ch=GetChar (); $ while(GetChar ()! =Ten) the return 0; the returnch; the}
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