0

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

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100

2 Answers2

0

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.

Community
  • 1
  • 1
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • i still do not understand. can you explain how this is printing two characters instead of just one like its supposed to? #include main() { int c; c=getchar(); while (c!= EOF) { putchar(c); c=getchar(); } } – firstlastnamename Jun 23 '15 at 06:27
  • can explain whats going on line by line of this program? thanks – firstlastnamename Jun 23 '15 at 06:31
  • @firstlastnamename , What's your input? You enter one character and press enter, the first character gets read by `getchar` while the newline character generated from the enter key press is consumed by the second call to `getchar`. – Spikatrix Jun 23 '15 at 08:43
  • which call to getchar are you referring to when you say "second call to getchar?" If you're referring to the getchar under the while function, wouldnt you be wrong since that getchar function is responsible for asking the user for a new character? And when I enter something other than a character such as a word like "test" how is this program able to spit back "test" if getchar only reads one character? Thank you – firstlastnamename Jun 23 '15 at 21:58
  • Second call to `getchar` is the one in the loop. When you enter "test", `getchar` outside the loop reads `t`. `putchar` in the loop prints it, `getchar` in the loop reads the next character `e`, `putchar` prints it, `getchar` reads the next character `s`, `putchar` prints it, `getchar` reads the next character `t`, `putchar` prints it, `getchar` reads the next character `\n`(newline character), `putchar` prints it. Then, `getchar` in the loop waits for further input as the loop hasn't ended as you haven't sent EOF. – Spikatrix Jun 24 '15 at 06:21
  • omg thank you so much. really... i love you man... you finally solved the problem thats been haunting me for literally two days... couldnt find one resource that could explain it as clearly as you did.... thanks... where did you learn this? I want to use the same resource. thanks – firstlastnamename Jun 24 '15 at 06:48
  • so a buffer is being used? what is this buffer we're even referring to? – firstlastnamename Jun 24 '15 at 06:49
  • "_where did you learn this?_" -- I learnt several stuff after programming in C for a year. I hadn't used online tutorials for C. Started with a (stupid) local book and then StackOverflow taught me the rest! :-) "_so a buffer is being used?_" -- Sort-of. As mentioned in my answer and the other, it is the [standard input stream(stdin)](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_.28stdin.29) . More info here: [What is the standard input buffer?](http://stackoverflow.com/questions/14068346/what-is-the-standard-input-buffer). BTW, glad I could help. You're welcome! :-D – Spikatrix Jun 24 '15 at 06:52
  • And you can [accept an answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) to credit the answerer and this also gives a small boost to your [reputation](http://stackoverflow.com/help/whats-reputation) – Spikatrix Jun 24 '15 at 06:55
  • I've been thinking more about this... the first T is scanned by the first getchar outside of the while and is printed by the putchar inside the while... Then i understand how the getchar will automatically ask for another letter and in this case how the "e" in test" will be placed there... but who presses enter for the getchar function to take in the e? thanks – firstlastnamename Jun 25 '15 at 05:03
  • is the answer to this above comment that there doesnt need to be another "enter' since all of the characters have been entered at once when the getchar first asked for a char? the getchar in the while gets satisfied by the E thats already been entered? Im assuming my understanding is correct? – firstlastnamename Jun 25 '15 at 05:05
  • Yes. Right. As said in my answer, `getchar` reads from the input stream called `stdin`. When you type characters, they are echoed on the terminal. To flush them all into the `stdin`, you'll have to press enter. After you press enter, the data you typed will be sent to the `stdin` and `getchar` will read one by one. – Spikatrix Jun 25 '15 at 05:29
  • one more question please. So I know "while((c=getchar())!=EOF) "will assign the entered character to the char C. But if we just had "while(getchar()!=EOF)", this still asks for a letter to assign to c. Am I correct in assuming that the getchar() function alone (without the c= as in c=getchar()) asks for a character. If i dont put c=, then the character inputed would just get stored nowhere? and getchar()!=EOF will simply ask for a character and see if it is EOF or not, and getchar()!=EOF would not store the character anywhere? thanks – firstlastnamename Jun 25 '15 at 21:47
  • also, im reading in K&R that getchar "returns" values. For EOF getchar would "return" -1. In what context is the word "return" being used. What is being returned from where to what and for what? Thanks – firstlastnamename Jun 25 '15 at 22:13
  • "_"while((c=getchar())!=EOF) "will assign the entered character to the char C_" to the __int__ `c`, not __char__. `getchar()` returns an `int`. "_If i dont put c=, then the character inputted would just get stored nowhere?_" The inputted character is not stored in a variable, but it is available for testing the condition(`!=EOF`). `getchar` returns values. This value is what you assign to `c` and if you knew what returning values mean, all the above queries will be easily understood. You'll understand more if you knew functions. Do you? BTW, `getchar` receives and returns an `int`. – Spikatrix Jun 26 '15 at 05:49
  • @firstlastnamename Google it. – Spikatrix Jul 11 '15 at 06:41
0

Yes, there is a buffer, hidden in the input stream. A "line" buffer to be precise: Characters are memorized in the stream until a newline is entered, and then sent to the process (at which point whatever blocking read function you're using -- be it getchar(), fgets(), or even read() -- returns).

In a loop calling getchar(), the function will keep returning characters from the stream until the newline character is returned, at which point the function will block again until there's another newline in the stream.

Medinoc
  • 6,577
  • 20
  • 42