Join Dan Gookin for an in-depth discussion in this video Using character I/O, part of Learning C (2014).
- Programs are known for both input and output. Output goes to the standards output device, which is usually the screen or a terminal window. Input comes from the standard input device, which is normally the keyboard. In this movie, I explore input and output concepts. These involve character I/O, which is the reading of single characters, as well as the output of a single character. I'll also touch upon the concept of stream input and output. The two must common C language character I/O functions are getchar and putchar, that's how I pronounce them.
Getchar fetches a character from standard input. Putchar sends a character to standard output. Both of these functions require the inclusion of the standard I/O header file stdio.h for their prototypes and such. Although, they are character functions, they work with integer values. Yes, that's weird, but so are so many things in the C language. In fact, you may find yourself frequently using char variables with these functions. When you do the compiler may gently warn you about the mistake.
Finally. these functions are stream oriented, which is something I'll explain later in the movie. Integer variable C is declared at line five. It's used with the getchar function at line eight, storing a single character from the input stream. In this example, that would be a character typed at the keyboard. The character is displayed in line nine. The invariable C is used, but the character place holder, resent C, is specified. This ensures that the character is displayed not it's code value.
Build and run the code. Note that the prompt keeps the cursor on the same line, and I added a space to make it more readable. Type a letter, such as a big Z, and press enter. Ta-dah. Modify the code so the putchar function outputs the character you typed. To do that you need to split the print F function in two, so I'm going to go ahead and edit it to where it says, "You Typed," and notice I'll keep that single quote right there. Close the string in parenthesis, and make it a statement with a semicolon.
Then I'll add putchar on the following line, and then display the rest of the string by using Print F, but the argument C is no longer needed. Instead, it appears up here in the putchar function where it's output directly. Save the source code file. This solution provides for the putchar function to generate output, but it also makes the code longer and less readable. That's actually O.K. in C, and you'll discover that the output is unchanged.
Build and run. This time I'm going to type in X and the output looks the same. You can also use the putchar function with immediate values. At this point in the code, the statement of line 11 really just outputs three characters, a single quote, a period, and a new line. I could change line 11 to read putchar. Putchar, parin, single quote, backslash, single quote, another single quote.
That looks odd, but you have to escape the single quote character, or the the compiler becomes confused. Escape characters are covered in an earlier movie. A second putchar function would display the period. The final putchar function would display the escape sequence, which is specified in the original print F function as a new line. Oops, I'll fix that or I'll get in trouble. And use single quotes.
So we have three putchar functions instead of a single print F function that displayed a three character string. Save these changes. Build and run. And the output is the same. Open exercise file CarIO2, here two getchar functions fetch two characters. These characters are then displayed by using a sequence of Print F and putchar functions. Build and run the code. I'm going to type an M and an N and press enter.
The two characters are stored in invariables A and B and then displayed. You'll note that the getchar functions do not pause and wait for input. They simply look for all input coming from the standard input device like a stream of characters flowing out of a hose. To demonstrate this phenomenon, run the program again. Just type Z and press enter. The weird output you see is the code displaying the new line character, which was stored in variable B.
Run the code again. This time type several letters and then press the enter key. A B C D E. The entire stream of characters, A through E on my screen, were fed into the program via standard input. The getchar function read only the first two, but my input persisted until I pressed the enter key. The stream input could also be demonstrated by examining exercise CharIO3. Here it looks like one character is fetched by getchar and then immediately displayed by putchar.
That's logical because C programs run top down, and they do, and that's exactly what happens in this program, but it's not what you see when the code runs. Build and run. Type A B C or any other three letters and press the enter key. The output happens all at once because of the stream. The characters you type are actually sent to output as you type them, which is how the code runs. But stream output is buffered.
That means that the computer waits until the buffer is full or flushed before sending out the characters. In this case, the buffer is flushed once the program is finished. I'll explore stream input and output further in later movies. For now, remember that the standard C I/O library functions are stream oriented.
- Understanding a C program
- Adding comments
- Using escape characters
- Working with values and placeholders
- Introducing variables
- Making a decision with if
- Adding functions
- Manipulating strings
- Building arrays
- Nesting structures