twitter
    Find out what I'm doing, Follow Me :)

Back To The File Named Simpleio.c

Let us continue our tour of the file in question. The one variable "c" is defined and a message is printed out with the familiar "printf" function. We then find ourselves in a continuous loop as long as "c" is not equal to capital X. If there is any question in your mind about the loop control, you should review Chapter 3 before continuing. The two new functions within the loop are of paramount interest in this program since they are the new functions. These are functions to read a character from the keyboard and display it on the monitor one character at a time. The function "getchar()" reads a single character from the standard input device, the keyboard being assumed because that is the standard input device, and assigns it to the variable "c". The
next function "putchar(c)", uses the standard output device, the video monitor, and outputs the character contained in the variable "c". The character is output at the current cursor location and the cursor is advanced one space for the next character. The system is therefore taking care of a lot of the overhead for us. The loop continues reading and displaying characters until we type a capital X which terminates the loop.

At this point we need to mention that 1616/OS, Unix and MS-DOS sometimes do different things, even in apparently simple areas such as this. In some version of HiTech C for the 1616/OS, characters are repeated as you type them, but are not repeated when you press the return key. This is because the "getchar()" function was redefined as the 1616/OS function "getch()", which is not precisely the same.

Compile and run this program on an MS-DOS machine for a few surprises. When you type on the keyboard, you will notice that what you type is displayed faithfully on the screen, and when you hit the return key, the entire line is repeated. In fact, we only told it to output each character once but it seems to be saving the characters up and redisplaying them. A short explanation is in order