Using the input function scanf

The input counterpart to printf is scanf. scanf is a function from the standard input and output library that allows you to read data from the computer keyboard. To make the standard input and output library available to your program, the following line must appear at the top of your program:

#include <stdio.h>

The general format for the scanf function is

scanf(control string, other arguments);

Note that the general format (or syntax) for the scanf function is very similar to the format for printf, although there are two important differences. First, the control string consists almost entirely of conversion specifiers, which determine the type of data read from the keyboard. Second, the other arguments portion of the statement consists of a comma-separated list of pointer expressions, or addresses.

DON'T PANIC! If you follow the directions below, you will be able to code and debug scanf statements with confidence, even if you don't know anything about pointer expressions and addresses.

The control string part of the scanf function tells the computer what type of data will be read from the keyboard. The type is designated, as in the printf function, by a set of conversion specifications. A conversion specification begins with a % and ends with a conversion character. The conversion character you choose is related to the type of data you will read from the keyboard. The different conversion characters are listed in the table below. (This table is also found in your text, A Book on C, page 500.)

Conversion Character
Type of data matched
ca character
da decimal integer
e, f, ga floating point number
sa string
Now that the scanf function knows how many and what type of data will be read from the keyboard, the other arguments part tells the scanf where to put the data. This is the "comma-separated list of pointer expressions, or addresses" part of the scanf function. At this point, all you need to know is to make a list of variable names that correspond to the number, order, and type of conversion specifiers in the control string. But here's the trick: precede each variable name with an ampersand (&). This will be more clear after looking at an example.

#include <stdio.h>

int main(void)
{
  int   i;
  float x;

  printf("What are your favorite numbers?\n");
  printf("(Enter an integer and a float.)\n");

  scanf("%d%f",&i,&x);

  printf("\nYour favorite numbers are %d and %f!\n",i,x);

  return 0;
}

The control string in the scanf function is "%d%f" because we want to input an integer and a float. The other arguments part of the scanf function is &i, &x. It is important to notice that the variables agree with the order and type dictated by the control string. The & that appears before each variable in the example is required. If you do not have an & in front of each variable, your program work correctly only if you are using pointers. Since we have not discussed pointers, always use an & in front of each variable. If you do not, the compiler should issue a warning about that line in your code. Here is the output from this program:


What are your favorite numbers?
(Enter an integer and a float.)
126  3.15

Your favorite numbers are 126 and 3.150000!

The text in red corresponds to what the user typed at the keyboard. There are additional examples in A Book on C on pages 18-21 and 499-503.

We will learn more about pointers later in the course.


last modified: