# A Little C Primer/C Console IO

This chapter covers console (keyboard/display) and file I/O. You've already seen one console-I/O function, "printf", and there are several others. C has two separate approaches toward file I/O, one based on library functions that is similar to console I/O, and a second that uses "system calls". These topics are discussed in detail below.
Console I/O in general means communications with the computer's keyboard and display. However, in most modern operating systems the keyboard and display are simply the default input and output devices, and user can easily redirect input from, say, a file or other program and redirect output to, say, a serial I/O port:
The program itself, "myprog", doesn't know the difference. The program uses console I/O to simply read its "standard input (stdin)"—which might be the keyboard, a file dump, or the output of some other program—and print to its "standard output (stdout)"—which might be the display or printer or another program or a file. The program itself neither knows nor cares.
Console I/O requires the declaration:
Useful functions include:
Windows-based compilers also have an alternative library of console I/O functions. These functions require the declaration:
The three most useful Windows console I/O functions are:
The "printf" function, as explained previously, prints a string that may include formatted data:
—which can include the contents of variables:
The available format codes are:
Using the wrong format code for a particular data type can lead to bizarre output. Further control can be obtained with modifier codes; for example, a numeric prefix can be included to specify the minimum field width:
This specifies a minimum field width of ten characters. If the field width is too small, a wider field will be used. Adding a minus sign:
—causes the text to be left-justified.
A numeric precision can also be specified:
This specifies three digits of precision in a field six characters wide.
A string precision can be specified as well, to indicate the maximum number of characters to be printed. For example:
This prints:
Similarly:
—prints:
And finally:
—prints:
Just for convenience, the table of special characters listed in chapter 2 is repeated here. These characters can be embedded in "printf" strings:
The "scanf" function reads formatted data using a syntax similar to that of "printf", except that it requires pointers as parameters, since it has to return values. For example:
There is no "&" in front of "name" since the name of a string is already a pointer. Input fields are separated by whitespace (space, tab, or newline), though a count, for example "%10d", can be included to define a specific field width. Formatting codes are the same as for "printf", except:
* There is no "%g" format code.
* The "%f" and "%e" format codes work the same.
* There is a "%h" format code for reading short integers.
If characters are included in the format code, "scanf" will read in the characters and discard them. For example, if the example above were modified as follows:
—then "scanf" will assume that the two input values are comma-separated and swallow the comma when it is encountered.
If a format code is preceded with an asterisk, the data will be read and discarded. For example, if the example were changed to:
—then if the two fields were separated by a ":", that character would be read in and discarded.
The "scanf" function will return the value EOF (an "int"), defined in "stdio.h", when its input is terminated.
The "putchar" and "getchar" functions handle single character I/O. For example, the following program accepts characters from standard input one at a time:
The "getchar" function returns an "int" and also terminates with an EOF. Notice the neat way C allows a program to get a value and then test it in the same expression, a particularly useful feature for handling loops.
One word of warning on single-character I/O: if a program is reading characters from the keyboard, most operating systems won't send the characters to the program until the user presses the "Enter" key, meaning it's not possible to perform single-character keyboard I/O this way.
The little program above is the essential core of a character-mode text "filter", a program that can perform some transformation between standard input and standard output. Such a filter can be used as an element to construct more sophisticated applications:
The following filter capitalizes the first character in each word in the input. The program operates as a "state machine", using a variable that can be set to different values, or "states", to control its operating mode. It has two states: SEEK, in which it is looking for the first character, and REPLACE, in which it is looking for the end of a word.
In SEEK state, it scans through whitespace (space, tab, or newline), echoing characters. If it finds a printing character, it converts it to uppercase and goes to REPLACE state. In REPLACE state, it converts characters to lowercase until it hits whitespace, and then goes back to SEEK state.
The program uses the "tolower" and "toupper" functions to make case conversions. These two functions will be discussed in the next chapter.
The "puts" function is like a simplified version of "printf" without format codes. It prints a string that is automatically terminated with a newline:
The "fgets" function is particularly useful: it reads a line of text terminated by a newline. It is much less finicky about its inputs than "scanf":
This program includes the "strcmp" function, which performs string comparisons and returns 0 on a match. This function is described in more detail in the next chapter.
These functions can be used to implement filters that operate on lines of text, instead of characters. A core program for such filters follows:
The "fgets" function returns NULL, defined in "stdio.h", on input termination or error.
The Windows-based console-I/O functions "getch" and "getche" operate much as "getchar" does, except that "getche" echoes the character automatically.
The "kbhit" function is very different in that it only indicates if a key has been pressed or not. It returns a nonzero value if a key has been pressed, and zero if it hasn't. This allows a program to poll the keyboard for input, instead of hanging on keyboard input and waiting for something to happen. As mentioned, these functions require the "conio.h" header file, not the "stdio.h" header file.