0

Here, I have understood why putchar() is printing only the first character that is 'C'

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

output : > Cprograming
         > C

But, When using a while loop the behavior of those functions is changing.

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

output : > Cprograming
         > Cprograming
         > Bells and Wistles
         > Bells and Wistles
         ...

Why suddenly getchar() and putchar() are storing and printing more than one character..?? Why is this happening..??

I tried to practice it this way.

int c,d,e,f,g;
    c = getchar();
    putchar(c);
    d = getchar();
    putchar(d);
    e = getchar();
    putchar(e);
    f = getchar();
    putchar(f);
    g = getchar();
    putchar(g);

This is my assumption how program might be working.

Start with c = getchar()

User enter 'Qwerty'

getchar() stores this 'Qwerty' one by one character in something called input buffer(sorry for technical words)

user hits enter, getchar() returns the first character from buffer and store it into c

putchar(c), sends this value to something called output buffer, Now output buffer has Q stored in it . Next, d = getchar(), now this new getchar(), goes to the input buffer asks for the top character stored in it and return it to 'd'

putchar(d), sends this new d value to output buffer, and now output buffer contains 'Qw'

--this repeats until we don't have enough getchar() & putchar() or we run out of characters.

In this case, we don't have enough getchar() & putchar(). So, the output screen will look something like this

Qwerty
Qwert

My question is,

Why d = getchar(); collects character from buffer. Why it does not just start a new stream for itself...?

I mean c = getchar(); says ok start typing
and

d = getchar(); No, don't start typing. first check if buffer has something left. if it doesn't only then start typing.

Why..?

I'm really sorry if I'm being absolutely silly. Just a beginner programmer. Hope you guys understand.

  • Because in the first program you read one char and you print one char and in the second program you read and print chars until you press Ctrl+B. – Jabberwocky Jul 08 '21 at 09:23
  • 3
    How are you able to move 100 bricks from one side to the other while you can only carry 1 brick at a time? The solution is the same as with your question. You do it one by one until you are done. You never carry more than 1 brick at any time. – Gerhardh Jul 08 '21 at 09:25
  • The first example does not have any loop, so it reads and outputs one character. The second example has a loop, so it reads and outputs until the end condition is met. Note that input is buffered, so you didn't get *any* output until you pressed Enter. – Weather Vane Jul 08 '21 at 09:33
  • It is a remarkable coincidence to see this question entered 13 hours after [this one](https://stackoverflow.com/questions/68292096/i-am-reading-the-c-programming-language-2ed-and-i-didnt-understand-a-concep/68292374#68292374). – Eric Postpischil Jul 08 '21 at 10:33
  • @PradakshPrarthan don't put code in comments, it's unreadable. But [edit] the question. – Jabberwocky Jul 08 '21 at 13:49
  • 1
    Please can somebody check the new edited question.. – Pradaksh Prarthan Jul 08 '21 at 14:59
  • 1
    Your _assumption how program might be working_ is right. _Why d = getchar(); collects character from buffer. Why it does not just start a new stream for itself...?_ The C standard demands that: __The `getchar` function returns the next character from the input stream pointed to by `stdin`.__ _I mean c = getchar(); says ok start typing and d = getchar(); No, don't start typing. first check if buffer has something left._ This perceived difference does not exist; the first `getchar()` does in exactly the same way _first check if buffer has something left_, it's just at the start it hasn't. – Armali Jul 08 '21 at 17:34
  • @Armali you should post your comment as an answer – Jabberwocky Jul 09 '21 at 05:41
  • That would be a good suggestion, hadn't Eric Postpischil already fired and forgot his single-handedly close weapon. – Armali Jul 09 '21 at 08:12
  • 1
    Thank you @Armali i got it now. It's clear to me now. I have one more question. Why 'int' is used and not float or char or any other declaration ? They mentioned to use int specifically, why so?? The also said -- "We must declare c to be a type big enough to hold any value that getchar returns. We can't use char since c must be big enough to hold EOF in addition to any possible char. Therefore we use int. "--. I didn't get the meaning of this. – Pradaksh Prarthan Jul 10 '21 at 06:15
  • If `char` has 8 bits and `EOF` the value −1, the character 0xFF and `EOF` in two's complement representation are indistinguishable after assigned to a `char`, so `char` is not _big enough_. - `float` would be a possible, but strange choice for an integer value. – Armali Jul 10 '21 at 07:44

2 Answers2

0

The while loop, re-executes the statements as long as the validation condition remains true. Delayed echo output illustrates buffered input, when the entered characters are accumulated and stored in a time domain called a buffer. Pressing a key -Enter- makes the block of typed characters available to the program.

Генс
  • 93
  • 5
-1

Getchar will take the input in a stream and when he does it he does not delete the stream.

The thing is he get a data then you print you check the while then you go in getchar then you read the next data in the stream

try it by yourself with this

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

and use random number you will see it will stop when he find a number with an ascii value more or equal to 51

Thibaud
  • 1,059
  • 4
  • 14
  • 27