Search code examples
casciieofnon-ascii-charactersstdio

What does putc() return in c?


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?


Solution

  • 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.