1

I was willing to know what does putc() return. I was going through the following link:https://www.tutorialspoint.com/c_standard_library/c_function_putc.htm. Now if putc() returns EOF on error, then a natural question is what does it return on end of file? for that I wrote the following in dev c++:

Code

#include <stdio.h>

int main() {
    char ch = EOF;
    printf("%d\n", putc(ch, stdout));
    printf("hello %d", EOF);

    return 0;
} 

Output

255
hello -1

This is a little bit weird. Can anyone help me out ? EOF is not an ASCII character as stated in What is the ascii value of EOF in c.? then why 255 (yes its not ASCII) in the first line and -1 in second?

chqrlie
  • 131,814
  • 10
  • 121
  • 189

3 Answers3

2

EOF is -1 as you already have probably already found out. So if you putc(EOF,stdout) then you see 255 in the output because -1 is converted to unsigned char before printing.

vasek
  • 2,759
  • 1
  • 26
  • 30
  • 2
    AFAIK, the standard doesn't require a specific value for `EOF`. `-1` is just commonly used, as it must be a value different from any character value as `unsigned char`. –  Jul 19 '17 at 12:19
  • 1
    @FelixPalmen: `EOF` must also be negative. – chqrlie Jul 19 '17 at 17:10
  • @vasek: The OP does not write `putc(EOF,stdout)`, he writes `putc(ch, stdout)` and `ch` is an `unsigned char` with value `(unsigned char)EOF`, `255` on his system. `putc(EOF, stdout)` would have the same beavior because the argument is converted as `unsigned char` as you wrote. – chqrlie Jul 19 '17 at 17:13
  • Actually `ch` was declared as `char`, therefore signed. – vasek Jul 19 '17 at 18:02
  • @vasek: oops you are correct! but the argument is converted to `unsigned char` anyway. And `char` might be unsigned by default, which after all is the only consistent choice ;-) – chqrlie Jul 19 '17 at 20:32
0

According to fputc documentation on cppreference.com, the character is internally converted to unsigned char:

Internally, the character is converted to unsigned char just before being written.

If you now have a (probably signed) character, and you assign char ch = EOF, and if EOF is actually -1, then converting (char)-1 into (unsigned char)-1 gives 255, which is then passed back as int.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
0

From a putc() manual:

fputc(), putc() and putchar() return the character written as an unsigned char cast to an int or EOF on error.

(emphasis mine)

EOF is an int constant that is different from any character value, so a natural choice could be -1 (and many implementations use this).

Now, if your char is signed, converting EOF to char will still be -1. But the function uses unsigned char for characters, so it returns 255 (this would be the same value interpreted as unsigned, assuming the signed value was stored in 2's complement and your char has 8 bits).


Just use the functions the way they are documented: Pass unsigned char for printing (a character constant is fine) and take the return value as an int. if the return value isn't EOF, then you can interpret it as an unsigned char.