Mechanics of Basic Input

The first program we examined in this course, quarts.c, started with a value for quarts and then computed and printed the corresponding number of liters:

/* A simple program to converts
   a number of quarts to liters
 */

#include <stdio.h>

int main ()
{
   int quarts;   
   double liters;

   printf ("This program converts quarts to liters\n");

   quarts = 2; 
   liters = quarts / 1.056710 ;
   printf ("%d quarts = %lf liters\n",
           quarts, liters);

   return 0; 
}

However, rather than specify the number of quarts directly in the code, the program would be more helpful if the user could type the desired quarts value each time the program is run.

Although quarts.c works fine for 2 quarts, its general use is cumbersome. If we want the liter equivalent for another number of quarts, we must:

Getting Simple Values from the User

To resolve the awkwardness found in quarts.c, we would like the program, as it is running, to ask the user how many quarts should be converted:

This program converts quarts to liters
Enter number of quarts (an integer):

The user then types a value, and the program continues with that entered value. An example is found in quarts-rev.c. When compiled and run, the interaction with the program will look something like this, assuming that the user will input 23 as the value for quarts:

This program converts quarts to liters
Enter number of quarts (an integer):  23
23 quarts = 21.765669 liters

C's scanf function provides a commonly-used mechanism for reading user input. Although the function's capabilities are sophisticated and full use of scanf requires substantial reading and practice, the following discussion will provide enough information for our needs at the start of this course. You should be able to use these examples to read single numbers (integers, floats, or doubles) for quite a few weeks. Expanded features of scanf are discussed in a later module, after readers have an understanding of several additional concepts and elements of C.

Basics of scanf

When reading data from the keyboard, the program must specify both the type of information and where to store that information. The scanf function collects these pieces following a format similar to what we have seen for printf. For the revised quarts program, the needed line is:

scanf ("%d", &quarts);
    

C interprets this line as follows:

When the computer encounters a scanf function while it is running a program, the computer waits for the user to type the data and hit the "enter" key. (Before hitting "enter", the user might recognize a typographical error, use the "delete" or "backspace" key to erase some characters, and the type a correction.)

Since users need to know the computer is waiting for them to type, it is common to include a printf statement before scanf to prompt the user to enter the needed information. You should also inform the user of what type of input is allowed.

printf ("Enter number of quarts (an integer):  ");

scanf Examples

Assume the following declarations:

int i;
double a;
double b;
double c;
    
scanf statement description
scanf("%lf", &a) read double value for a
scanf("%lf %lf", &a, &b) read two double values for a and b
scanf("%d %lf", &i, &b) read integer value for i and double value for b
scanf("%lf %d", &a, &i) read double value for a integer value for i

quarts-rev.c

/* A simple program to convert a number of quarts to liters
   Version with user input
*/

#include <stdio.h>

int main ()
  { printf ("This program converts quarts to liters\n");  

    int quarts; 
    double liters;

    printf ("Enter number of quarts (an integer):  ");
    scanf ("%d", &quarts);
    liters = quarts / 1.056710 ;

    printf ("%d quarts = %lf liters\n", quarts, liters);

    return 0;
  }

Notes for quarts-rev.c