2
#include<stdio.h>

int main()

{


    int c;

    while((c=getchar())!=EOF)
       putchar(c);
    putchar(c);     //2nd putchar     
    getch();


}

In the above code from the book 'C by Ritchie n Kernighan', the reason for declaring c an int variable is given as :

so that c may be able to hold the code of EOF which is outside the char codes which a char datatype can accommodate.

But when I declare c as a char, and input the EOF(CTRL-Z + Enter),that value(which is -1 on my computer)does go into c and I immediately end up exiting the while up, just the way it happens if I declare c as an int. Why do we need to declare c as an int, when char works just fine?? or am I missing something here?

One more thing, in the second putchar statement, it should print the last value of c (ie. -1) after having received EOF but it doesn't. Why?

haccks
  • 104,019
  • 25
  • 176
  • 264
kasif
  • 57
  • 1
  • 8
  • 2
    Answered here http://programmers.stackexchange.com/questions/197625/storing-the-eof-end-of-file-character-in-a-char-type – Pavan Manjunath Jun 24 '14 at 08:40
  • Keep your program out of reach from anybody speaking Dutch. Which has the ij digraph as a common letter, often with character code 255. Not much of a problem, there are only 25 million of us. – Hans Passant Jun 24 '14 at 09:09
  • To address your question in the last paragraph: Before `putchar` prints a value it casts it to `unsigned char`. So, assuming `EOF` is -1 (what it doesn't have to, but most often is), a cast to an (8-bit) `unsigned char` yields `0xFF`, what, depending on your encoding, might be a valid character or not (e.g. in UTF-8 this byte can't occur in any valid sequence). So maybe you just don't see it, although it is outputted. Pipe the output through `hexdump` or write it into a file. – mafso Jun 24 '14 at 12:37
  • that's a revelation! @HansPassant – kasif Jun 24 '14 at 17:18
  • Possible duplicate of [Difference between int and char in getchar() and putchar()?](http://stackoverflow.com/questions/35356322/difference-between-int-and-char-in-getchar-and-putchar) – Antti Haapala -- Слава Україні Feb 13 '16 at 09:14

2 Answers2

3

Whether char is signed or unsigned is implementation-defined.

If char is unsigned, then no char value can be equal to EOF, the loop will never exit.

If char is signed, then EOF could be equal to c, this is what happens on your machine. But the problem is, this also means, a valid char could be equal to EOF, causing the loop exit too early.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
0
But when I declare c as a char, and input the EOF(CTRL-Z + Enter),that 
value(which is -1 on my computer)does go into c and I immediately end up 
exiting the while up, just the way it happens if I declare c as an int.

Recall, getchar is the equivalent of getc(stdin). When you reach eof, that condition is set on stdin your test within the loop can no longer be true. So the loop terminates regardless of whether you use an int, a char or and unsigned char.

One more thing, in the second putchar statement, it should print the 
last value of c (ie. -1) after having received EOF but it doesn't. Why?

For the same reason, EOF is reached, the loop terminates without calling putchar.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85