C: printf() and putchar() questions

I was reading K&R book and wanted to test out printf() and putchar() functions in ways that I never tried. I encountered several unexpected events and would like to hear from more experienced programmers why that happens.

char c;
while((c = getchar()) != EOF) {
//put char(c);
    printf("%d your character was.n", c);
}
  • How would one get EOF (end of file) in the input stream (getchar() or scanf() functions)? Unexpected key that is not recognized by getchar()/scanf() function could produce it?
  • In my book, it says that c has to be an integer, because it needs to store EOF and the variable has to be big enough to hold any possible char that EOF can hold. This doesn't make sense for me, because EOF is a constant integer with a value of -1, which even char can store. Can anyone clarify what was meant by this?
  • What happens when I send "hello" or 'hello' to putchar() function? It expects to get an integer, but returns weird output, such as EE or oo, if I send the latter string or char sequence.
  • Why when I use printf() function that is written above I get two outputs? One is the one I entered and the other one is integer, which in ASCII is end of line. Does it produce the second output, because I press enter, which it assumes to be the second character?
  • Thanks.


  • on Linux, you can send it with Ctrl+d

  • you need an int, otherwise you can't make the difference between EOF and the last possible character (0xFFFF is not the same than 0x00FF)

  • putchar wants a character, not a string, if you're trying to give it a string, it'll print a part of the string address

  • you only get one output: the ascii value of the character you entered, the other "input" is what you typed in the terminal

  • edit - more details about 2

    You need an int, because getchar can returns both a character of value -1 (0x00FF) and an integer of value -1 (0xFFFF), they don't have the same meaning: the character of value -1 is a valid character (for instance, it's ÿ in latin-1) while the integer of value -1 is EOF in this context.

    Here's a simple program that shows the difference:

    #include <stdio.h>
    
    int main(int argc, char ** argv) {
        {
            char c = 0xFF; /* this is a valid char */
            if (c == EOF) printf("wrong end of file detectionn");
        }
        {
            int c = 0xFF; /* this is a valid char */
            if (c == EOF) printf("wrong end of file detectionn");
        }
    }
    

    The first test succeeds because 0xFF == -1 for char, while the second tests fails because 0x00FF != -1 for int.

    I hope that makes it a bit clearer.


  • you must close the input stream to get EOF. Usually with CTRL-D in UNIX, but see you tty config (stty -a)
  • already answered
  • same
  • your tty echoes what you type by default. If you don't want this, set it in noecho mode (stty -echo). Becareful as some shells sets it again to echo. Try with sh. You must be aware taht tty also buffers your inputs until RETURN is types (see stty manual for raw mode if you need).
  • 链接地址: http://www.djcxy.com/p/31536.html

    上一篇: getline如何与cin一起工作?

    下一篇: C:printf()和putchar()问题