Guides:C/C Crash Course/Interacting with the user

From CoderGuide

Jump to: navigation, search

Back to TOC

Interacting with the user=

This section is divided up into several parts:

Introduction

Whew! Now that we've covered the basic elements of the C language, we can start talking about how to make programs that actually do something useful for a change.

One of the things that makes a program useful is it's ability to interact with the outside world. In this case, the user. When we display something on the screen or send it to a printer, we call this output. When we read something from the keyboard, or some other source, we call this input. What we're going to discuss now is console input-- the no frills, plain text input and output that was once our only way to interact with the computer. This interface is also the simplest to program for, so that's the one we will be covering (99% of the programs out there don't need a graphical interface, and most need no human interface at all). Other books cover programming for the windowing systems on different systems (Windows, Unix, and Mac, all have their own weird way of handling GUIs).

You'll hear a few terms: standard output (stdout), standard input (stdin). They basically refer to the standard way a program receives input and output, which is usually through the console (the terminal screen and keyboard). Some operating systems (notably Unix-like operating systems, and, to some extent, DOS and Windows), allows redirecting the standard output to a file, or standard input from a file or another program. That's beyond the cope of this document, so we won't get into that. But they usually use the < symbol to direct input into a program from a file, and the > symbol to direct output to a file. The | symbol (pipe), is used to direct output from one program into another program (On Unix systems, the programs run simultaneously).

We refer to standard input and standard output as stdin and stdout, respectively. These are called input and output streams, or simply I/O streams and are buffered. The buffered nature of these streams isn't important to think about until you're reading data from stdin because the program won't continue running until after the enter/return key is pressed-- even if you're trying to read one character (there are some operating system specific functions which allow you to bypass this buffer and read, and test for text in real time). This means that, after you read one character, extra characters may still be in the buffer. There's a very simple solution to this problem, which we'll talk about in a minute.

Personal tools