2
#include <stdio.h>
int main(){
    char ch;
    while((ch=getchar())!=EOF){
       putchar(ch);
    }
    char ch2 = 'A';
    printf("ch2=======>%c\n",ch2);
    ch2 = getchar();
    printf("ch2=======>%d\n",ch2);
    return 0;
}

I don't understand why it skips the ch2=getchar() input, and I get ch2 == -1 which is the value of EOF. I tried to solve this by adding another getchar() before ch2=getchar(), but I still get ch2 == -1. Why is it and how to fix it? Thanks for helping.

I'm using MacOS.

Rick
  • 7,007
  • 2
  • 49
  • 79
  • Why do you even use EOF in this example? As far as I can see it, this code has nothing to do with a File. – Meik Vtune Jul 30 '16 at 10:16
  • Last input is `EOF` so it wont go inside the loop; – Morteza Tourani Jul 30 '16 at 10:29
  • 3
    Once you've reached the end of input, any further attempt to read will still hit end of input. – melpomene Jul 30 '16 at 10:32
  • 2
    Also, don't store `getchar()`'s return value in a `char`. It returns `int` for a reason. – melpomene Jul 30 '16 at 10:32
  • @melpomene oh thanks. So does that mean the standard input stream permanently stop as far as I typed EOF? – Rick Jul 30 '16 at 10:47
  • @MeikVtune In fact I was trying to store some text input from a while loop, using getchar and a char array and then use `scanf` to read in another input character but I found it skipped, as same as the situation here. – Rick Jul 30 '16 at 10:49
  • @TianqingPeng: If you *typed* a Ctrl-Z, then it probably isn't. If you redirected stdin, e.g. with "< somefile.txt", then it is. – Rudy Velthuis Jul 30 '16 at 15:33
  • If I understand you correctly, your having a problem with a call to `getchar()`. I expect that you have read the man page for `getchar()`; But the posted code does not reflect an understanding of what the man page states. For instance, the returned type from `getchar()` is `int`, NOT `char`. – user3629249 Jul 31 '16 at 01:44

2 Answers2

1

When compiling, always enable all the warnings, then fix those warnings

I used this version of your code, which cleanly compiles:

#include <stdio.h>
int main(){
    int ch;
    while((ch=getchar())!=EOF){
       putchar(ch);
    }
    int ch2 = 'A';
    printf("ch2=======>%c\n",ch2);
    ch2 = getchar();
    printf("ch2=======>%d\n",ch2);
    return 0;
}

then entered:

ddd<ctrl-d>ccc<cr>

this is the expected (and actual) result:

ddd
ddd
ch2=======>A
ccc
ch2=======>99

Notes:

1) getchar() does not return until a <cr> or <ctrl-d> is entered

2) all entered characters are echo'd by the terminal driver (not the program)

3) there are still 2 cs and a <cr> in the input buffer that have not been consumed

user3629249
  • 16,402
  • 1
  • 16
  • 17
0

There is nothing surprising. Your loop

while((ch=getchar())!=EOF){
       putchar(ch);
}

exhausts stdin completely. Then printf() adds some data to stdout, but stdin is still empty, so subsequent getchar() returns EOF.

Sergio
  • 8,099
  • 2
  • 26
  • 52
  • 1
    Yes, well, but if stdin is input from the keyboard (as usual), even after typing Ctrl-Z, you can usually continue typing, and stdin is not really exhausted. – Rudy Velthuis Jul 30 '16 at 20:33
  • @RudyVelthuis Time slice between last loop iteration and `ch2 = getchar();` is so small that user surely can't provide new keyboard input and STDIN still may be considered to be empty. – Sergio Jul 30 '16 at 20:47
  • getchar() is, AFAIK, buffered, so the program will wait for input, until someone presses return (or whatever the key is called). The time slice should be irrelevant. – Rudy Velthuis Jul 30 '16 at 21:06
  • OK, I tried in Windows and on the Mac. The Mac doesn't indeed wait and simply returns -1 (EOF) immediately. That matches what you write. But on Windows, getchar() waits until the first `'\n'` is entered. Hmmm... Not consistent. I don't know which is actually correct. – Rudy Velthuis Jul 30 '16 at 21:36
  • @RudyVelthuis Yes.. I tried on Windows just now and my code works well. It waits for the `getchar()` to input but on Mac it simply returns -1. Lol, I am so confused. – Rick Jul 31 '16 at 02:55