27 October 2012

Part 4 - Customizing the Command Prompt, Standard Input/Output

Topics Covered

Customizing the command prompt in Windows
Standard input and output
Getchar(), putchar(), Pipes

Watch Video Part 4a >>

Watch Video Part 4b >>

Downloadable files



1. Using the Pseudocode provided (in2out.txt) and the descriptions of the functions getchar() and putchar(), construct a C program in2out.c which copies each character from standard input into its standard output.

2. Try to run in2out with various redirection operators. Redirect input to come from a file, or redirect output to go to a file, and try both together. Try using in2out in conjunction with the "pipe" `|' operator.

3. In a previous part we made the program fahr.c to convert a range of temperatures given at the command line. Run this program with the following parameters: fahr.exe 0 300 20 . Fahrenheit temperatures from 0 to 300 should appear increasing in steps of 20. What happens if you run fahr.exe 300 0 20 ?  Make appropriate changes to fahr.c such that it works in the forward direction as well as in the backward direction. That is, fahr 0 300 20  should provide a normal temperature table and fahr 300 0 20  should provide a table going backwards from 300 down to 0, decreasing in steps of 20.


1. Run the program in2out without any redirection operators. Any input typed at the keyboard will be echoed to the screen. One way to end this program from the keyboard is to signal the end of file with the Ctrl-Z keyboard shortcut. What other ways can you find to end this program (without closing the command prompt)?

2. Look up the manual page for the function getchar(3). When we write getchar(3), we mean the manual page section 3 of getchar. This function reads a single character, but its return type is int. Why do you suppose the return type was defined as int rather than as char, as might be expected?

Quick reference

Standard input

An input stream which normally comes from the keyboard but can be redirected to come from a file (using the < operator) or from a different program (using the | operator). The C standard library function getchar() will read a character from the standard input.

Standard output

An output stream which normally goes to the screen but can be redirected to go to a file (using the > operator) or to a different program (using the | operator). The C standard library function putchar() will output a character to the standard output.


A special marker to denote the end of a file or the end of an input stream. In many C implementations, this marker has an integer value of -1. The symbol EOF is a symbolic constant to represent this integer value in a standard way. If standard input is connected to a file, the operating system signals an end-of-file condition when the end of the file is reached. If standard input is connected to a keyboard, the end-of-file may be signalled by a special keyboard sequence which is chosen by the operating system.

No comments:

Post a Comment