Note
- Standard IO are bufferd
- One is full buffering,this is,it will not output to the specified file until the buffer is full
- Another buffer is line buffer,which is not cleared until a newline is encountered.How to define end of file? C use EOF to define the end of file.So why use EOF to define the end of file ,because the decimal number corresponding to character is not -1,and EOF is defined as -1.
// Program echo
// What you type,what the program returns to you.
#include <stdio.h>
int main()
{
int ch;
int start=1;// Used to mask whether to output leading character
while ((ch=getchar())!=EOF) {
if (start) {//if true then output leading character.
putchar('>');
}
//Because keyboard input is line-buffered,it is checked whether
//character ,the leading flag is set to true and a newline is
//the character is a newline character when reading .If it is a newline output.
if (ch=='\n') {
start = 1;
putchar(ch);
continue;
}
// if the character is not newline,then output and set leading flag as false.
putchar(ch);
start=0;
}
// getchar encounter EOF when you type ctrl+d
puts("end of echo");
return 0;
}
Result: