From the course: C: Data Structures, Pointers, and File Systems

Using single character I/O - C Tutorial

From the course: C: Data Structures, Pointers, and File Systems

Start my 1-month free trial

Using single character I/O

- [Instructor] A basic form of input and output involves characters. With the C language, characters come from the standard input device, usually the keyboard, and are sent to the standard output device, usually a terminal window. In this exercise file, the putchar function sends five single character constants to standard output, one statement after the other. Build and run. Hello. I use the putchar statement in line 10 often in my code to generate a new line in the output. The putchar function is a macro to find in the stdio header file included in line one and it's really a form of the putc function which you see in this exercise file which is the same as the proceeding one but using the putc function instead. This function requires a second argument shown here as stdout which is the standard output device and the output is going to be the same. Hello. Character input can be provided by the getchar function as shown in this exercise file. The function requires no arguments but it returns an integer value. Therefore at line five, variable ch is defined as an integer, not a character variable. Its value is read from standard input at line eight and then output in the printf statement at line nine. Let's build and run. And I will type a k, character k received. Characters read from standard input flow from the stream which isn't interactive. It doesn't work like programs you're familiar with. In this exercise file you might assume that one character's read and there's a pause and then the other character's read because there's two input statements at lines eight and 10. Build and run and I'm going to type an a, press Enter. What you see in the output is the enter character typed after the letter a. It's read from the stream and supplied for the second getchar statement and displayed here in the output. Now if you run the code again, I'm going to type two characters AB and press Enter and now you see that both characters were received. This exercise file is written to anticipate stream input. Two getchar statements appear one after the other using two variables, ch1 and ch2. So now the user knows to type two characters. But still, if you type one, the second character enter appears in the output like that. And just like the putchar function, the getchar function is a macro. Here you see the getc function equivalent. The function requires a single argument listing the input source and in this case at lines eight and nine, you see the standard input device listed. Finally I want to address why the getchar returns an integer and not a character value. In this exercise file, you see what I call a typewriter program. A character's read from input and then it's sent to output. At line 10, the character input is compared with the EOF constant which is an integer. EOF is the value returned when the end of file is read. Build and run. And you can type away. Hello there. Now when you press Enter, the stream is processed so this text you see here is what's called local echo or full duplex. And there's the string as it processed. Now if you want to terminate the program, you have to type the EOF character which is Control + Z in Windows or Control + D in Unix. So, I'll press Control + Z since this is Windows and you see the process terminates. Now at line five, if integer variable a was really character variable a, it might not detect the EOF. Especially if input were redirected from a file. This is the reason why both the getchar and putchar functions work with integer values, not characters.

Contents