cputchar

I used the function "putchar()" to print the result of a test, but i got a question mark instead


So like what the title suggest i have tried to use putchar() to print a result of a not equal to test != but the output i got is a question mark.

Here is the code:

#include <stdio.h>
main()
{

    int c;
    c = getchar() != EOF;
    putchar(c);
}

I have used printf() and it works:

#include <stdio.h>
main()
{

    printf("%d",getchar()!=EOF);
}

My question is: Why it doesn't work with putchar?


Solution

  • First, accepting that the comparison getchar()!=EOF will yield a Boolean value, which will be converted to either 1 (for true) or 0 (false) when interpreted as any integral type, the statement:

    printf("%d",getchar()!=EOF);
    

    prints the value of this conversion as a formatted integer - so you will see "1" or "0" printed.

    However, the statement:

    putchar(c);
    

    outputs the the actual character represented by the value of c (frequently, but not necessarily, the ASCII value). The characters represented by 0 and 1 are not 'printable' characters, so your console will display something indicating that - in your case, a question mark.