// 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:
原文:https://www.cnblogs.com/tailiang/p/12251794.html