0

we were asked to make a small program that would print each char from stdin using getchar()... I thought the following code would work but when i run it in windows console nothing happens, the cursor hangs indefinitely as if it's waiting for input...

int c = 0;
while(c != EOF)
{
    c = getchar();
    printf("%c\n", c);
}
printf("\nOut of loop!\n");

i thought the code would print the stream char by char, and if there was nothing in stdin, getchar() would return EOF and the loop would stop.. i think i'm misunderstanding something about how input is done in C, for a beginner it's really confusing... Any help!


Another confusing example:

char str[100]={0};
printf("Entrer a string: ");
scanf("%s",str); //i'll enter a string with spaces to have something to print in the loop
//let's say i enter Hello Wor^Zld!
int c = 0;
while(c!=EOF)
{
    c = getchar();
    printf("%c",c);
}
printf("Finally done with loop!!\n");


when i run the above code i get the following display in the console:
Wor->
with the same old cursor hanging waiting for input... Any idea why is that? it seems that Ctrl-Z+Enter "^Z-Enter" stopped the display but the loop continues? i'm honestly doing my best to understand but i got to be honest it's confusing.. Thank you in advance for helping and bearing with me!

360NS
  • 176
  • 1
  • 10
  • 3
    Yes, it *is* waiting for input from `stdin` (typically the keyboard), unless you redirect the input from a flle. The function will not return `EOF` until you type `Ctrl-D Enter` (Linux) or `Ctrl-Z Enter` (Windows). – Weather Vane Jan 16 '17 at 20:43
  • 3
    minor note : `while(c != EOF) { c = getchar();` --> `while((c = getchar()) != EOF) { ` – BLUEPIXY Jan 16 '17 at 20:45
  • @BLUEPIX yeah it's the same, but as a beginner i would avoid putting everything in a loop :P – 360NS Jan 16 '17 at 20:54
  • try `prog < prog.c` . In the case of `while(c != EOF)`, `EOF` will be printed. I don't think it is the desired behavior. – BLUEPIXY Jan 16 '17 at 20:55
  • @WeatherVane so upon first execution, getchar() will wait for input from keyboard instead of returning EOF when stdin is empty? could you please give more explanations? – 360NS Jan 16 '17 at 20:58
  • Your program works for me. Note that stdin may be buffered. On my system the program received no input until I pressed enter, then the entire line was processed. It exited correctly when it reached the end-of-file on stdin. – dsh Jan 16 '17 at 20:58
  • 2
    @user7427260 re your comment to BLUEPIXY, it is not the same, it is valuable advice to check the input *before* you process it. – Weather Vane Jan 16 '17 at 20:58
  • As I implied at the first comment. `getchar` is a **blocking** function that will not return unless you specifically type the `EOF` key sequence. If the input was redirected from a file, that will happen anyway. – Weather Vane Jan 16 '17 at 21:02
  • @BLUEPIXY you were right about checking for input, thanks – 360NS Jan 16 '17 at 21:07
  • Here is a [relevant question](http://stackoverflow.com/questions/2408140/confused-about-getchar-function) . Not all its answers are good. For example the value received must be `int` type, for that is what is returned. Have you yet even **read the man page for `getchar`?** – Weather Vane Jan 16 '17 at 21:07
  • @WeatherVane thank you sir, i think i understand now the behaviour of getchar().. if you would care to post an answer i'll accept it.. thanks anyway – 360NS Jan 16 '17 at 21:09
  • @user7427260 thank you, but this ground has been covered so many times ;~). Perhaps someone more worthy can work the comments into an answer. – Weather Vane Jan 16 '17 at 21:14
  • @WeatherVane: Actually to send EOF on Linux it needs to be `Enter Ctrl-D` (not the other way round). But you don't actually need to send EOF - just ensure some data gets sent through; pressing Enter is generally enough. – psmears Jan 17 '17 at 07:19
  • @psmears that is what I wrote in the first comment - and the snippet in the question is waiting for `EOF` to end the loop, is not checking for `Enter` at all. – Weather Vane Jan 17 '17 at 08:03
  • @WeatherVane: No, you wrote `Ctrl-D Enter`, which is (subtly) different! No, the reason it's not printing anything is not because it's checking for `EOF`, it's because stdin is line-buffered, so `getchar` blocks until you press `Enter`, and then returns data. – psmears Jan 17 '17 at 08:39
  • @psmears I thought you meant I had the Windows and Linux keys reversed. But it should be `Enter Ctrl-Z Enter` in the Windows case, can't check Linux. – Weather Vane Jan 17 '17 at 08:48
  • @psmears you mean that the sequence should be after a newline? would you please go through the question above again because i've added another example and maybe this is related to your comment – 360NS Jan 20 '17 at 23:25
  • What are you running on - Windows, Linux, some other Unix, DOS, something else? – psmears Jan 20 '17 at 23:28
  • thanks for the prompt reply, i'm using windows – 360NS Jan 20 '17 at 23:30

1 Answers1

0

You should fflush your output if you want be sure that it's print when you want. You should test EOF before try to print your character.

int c;
while((c = getchar()) != EOF)
{
    printf("%c\n", (char)c);
    fflush(stdout);
}
printf("Out of loop!\n");
Stargateur
  • 24,473
  • 8
  • 65
  • 91