Search code examples
cgetchar

How c=getchar is using buffer to out textstream when I input text stream?


I understand how this program shows how getchar is using buffer to copy and paste more than one character

#include <stdio.h>
main()
{
    int c;
    c=getchar();
    putchar(c);
    c=getchar();
    putchar(c);
}

but how does this code below

#include <stdio.h>
main()
{
    int c;

    c=getchar();
    while (c!= EOF)         // how does this program copy 12 and output 12. is a            
    {                           buffer being used? How so? 
       putchar(c);
        c=getchar();
    }
}

show a buffer being used... i dont get it and i dont see how its able to print 12 when i input 12. im new to C


Solution

  • getchar reads character by character from the standard input stream(stdin). The thing is, the terminal doesn't flush the typed data into the stdin until you press Enter. When you press it, characters are sent to the stdin with getchar reading each character and putchar outputting each one of them until EOF.

    is a buffer being used?

    No.... But sort-of.