[C]C语言的缓冲

Note

  1. Standard IO are bufferd
  2. One is full buffering,this is,it will not output to the specified file until the buffer is full
  3. 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.

[C]C语言的缓冲

// 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:
[C]C语言的缓冲

上一篇:5远程服务器操作(SSHLibrary)


下一篇:tomcat启动(一)startup.bat|catalina.bat分析