Jump to: navigation, search

Chapter 3 - Capturing input

The standard input stream

Standard C has a very primitive input/output model. It knows nothing of text boxes, check boxes, dialogs, radio buttons, or any of the input mechanisms that we have learned to expect over the last couple of decades. (It is a measure of C's power and flexibility that, despite this apparent limitation, many of those input mechanisms have been implemented in C.)

Although many modern C implementations do give you programmatic access to a sophisticated windowing system, the C language itself simply provides a standard way to process streams of data, where a stream is typically connected to some kind of file. There are, however, three special streams, known as the standard input stream, the standard output stream, and the standard error stream. We have already used the standard output stream to write characters. In this chapter, we will learn how to capture characters from the standard input stream.

The standard input stream is normally connected to a keyboard device (although it doesn't have to be).

The getchar function

The simplest function for retrieving data from the standard input stream is the getchar function. The prototype for getchar is listed in stdio.h, and looks like this:

int getchar(void);

The purpose of the getchar function is to retrieve a single character of data from the standard input device, returning its value. For this information to be useful to us, we need a way to place that return value into some kind of program object that will store it for us until we need it.

So we need two new concepts here: a way to define a program object of the right type, and some syntax for taking the value returned by getchar and placing it into that program object.

Defining an int object

Conceptually, an object is rather like the 'memory' button on your pocket calculator. If you switch on your calculator, type in a number (say, 6), and then press the M+ button, the value is stored in the calculator's memory. You can then enter another number (say, 42), press M+ again, and 42 is added to the 6 you entered before, giving 48, which is then stored in the calculator's memory, replacing the 6 that was there previously.

Unlike a cheap calculator, though, C doesn't just give you one memory location, but (on a modern computer) millions of locations - as many as you could reasonably want.

We can make it easy to use a memory location by assigning a name to it. When we do this, in C parlance we are defining an object.

Every object in C has a type. One of the most basic types in C is the int type. To define an object with this type, we need only choose a name for it and write that name in our function, preceding it with the keyword int, as follows:

int main(void)
{
  int myobject;  /* Definition */

  return 0;
}

The line int myobject; is actually rather busy.

The word myobject is not a C word. It's a word that I chose myself, and it acts as an identifier for a memory location. The line defines myobject to be an object of type int, which means it can hold an integer value within a range defined by your implementation. It can certainly store any integer value in the range -32767 to +32767, and in practice this range is likely to be very much wider.

The myobject identifier exists (in this case) for as long as the function in which it is defined. There are ways of reducing or indeed expanding this lifetime, but they need not concern us for now.

Assigning a value to an int object

To assign a value to our int object, we use the assignment operator, which we write using the = symbol.

int main(void)
{
  int myobject;  /* Definition */

  myobject = 6; /* Assign 6 to myobject */

  return 0;
}

We can, if we choose, shortcut the process of assigning an object in this manner, as follows:

int main(void)
{
  int myobject = 6;  /* Definition and initialisation */

  return 0;
}

When an object is given a value at the same point where it is defined, the process is called initialisation.

The assignment operator, the = symbol, is rather more powerful than we have so far seen, however. For example, it can be used to capture the return value of a function:

int six(void)
{
  return 6;
}

int seven(void)
{
  return 7;
}

int main(void)
{
  int myobject = six();  /* Definition and initialisation */

  /* at this point, myobject has the value 6 */

  myobject = seven();

  /* myobject now has the value 7 */

  return 0;
}

In the above program, the myobject object is initialised using the return value of the six() function, which returns the value 6. The initialisation process copies this value into myobject at the time of its creation (the association of the name with the memory location).

Then we stirred things up a bit by using the assignment operator to copy into myobject the return value of the seven() function. This operation overwrites (replaces) the value 6 with the new value 7.

We are now in a position to capture input!

Calling the getchar function

Here is a program that waits for you to type a character. When you have done so (and pressed ENTER to terminate the input), the program will capture that value, and display it for you. (Whether you see anything as a result will depend on what character you typed. Spaces aren't very visible, but letters and digits will show up just fine.) At present, the program will only be able to echo one character, but that's okay for now.

/* primitive_input_and_output.c */

#include <stdio.h>          /* prototypes for getchar and putchar */

int main(void)              /* Introduces main */
{                           /* Beginning of main's function body */

  int mycharacter;          /* mycharacter is defined, but given no initial value */

  mycharacter = getchar();  /* retrieve a character from the standard input device */

  putchar(mycharacter);     /* display the character we retrieved */

  putchar('\n');            /* remember, the standard output device needs a newline
                               character to be written before anything is displayed.
                             */

  return 0;                 /* terminate the function (and the program), returning
                               the value 0 to the caller
                             */
}                           /* closing brace terminates the main function body */

Now it's time to run this program. To do this, you will of course need to copy it (if you type it instead of using copy-and-paste, be very careful!) into your text editor, and then use your compiler to translate it into a machine program.

When you run the program, it will simply wait for you. Type a letter or a digit or a punctuation symbol, and then press the ENTER key. The program will then repeat your character back to you.

Summary

In this chapter, you learned about objects, assignment, initialisation, and the getchar function.

In the next chapter, we will learn how to do some very basic arithmetic.

Progress

To keep the progress list from becoming too long to be useful, items that you learned several chapters ago will occasionally be dropped.

Terminology
  • object
  • type
  • value
  • definition
  • assignment
  • initialisation
  • standard input
  • standard output
  • standard error
  • preprocessor
  • standard library function
  • user function
  • control flow
  • function
  • main
  • parameter list
  • statement terminator
  • function prototype
Syntax
  • comments
  • types
  • operators
    • assignment operators
      • the = operator
Standard library functions, by header
Personal tools