Solution for Exercise 7-3

ex0703

#include <stdio.h>

int main()
{
    int c;

    printf("I'm waiting for a character: ");
    c = getc(stdin);
    printf("I waited for the '%c' character.\n",c);
    return(0);
}

Notes

* Even though getchar() is really a lowly macro, it's still a popular "function" in the C language.

* The definition of getchar() can be viewed by examining the stdio.h header file. Here is what I see on my computer:

That's not exactly what's found in the stdio.h header file; I cleaned it up a bit, but it's effectively how getchar() is defined. Yes, it's created just like a constant; when the compiler builds the object code file, all references to getchar() are changed to getc(stdin).

* stdin is defined as a constant for the standard input device. Even though it's lower case, it's a constant.

* The standard output device is defined as the stdout constant.

* A third standard device exists, stderr, which is the standard error output device. It's designed for output of error messages. Unlike stdout, the stderr device is not affected when program output is redirected by the operating system. That way error messages sent to stderr appear on the standard output device (the console or screen) regardless of what nonsense is going on at the command prompt.