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:
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.
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): ");
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 |
/* 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; }
scanf
and printf
are defined in the stdio
library, so the same include
statement covers both functions.This program converts a number of quarts to liters Enter number of quarts (an integer):
and pauses with the cursor after "(an integer)". The user then can type on the same line as the prompt. (Although not necessary — adding "\n" will work fine, users may appreciate having the prompt at the start of the line where they are to type.)
quarts
is an integer, the scanf
format
specifies "%d". As discussed later in the course, integers and doubles are
stored in different ways, so the computer needs to know what internal
format to use when interpreting user input.scanf
skips over any
initial white space (e.g., spaces, tabs, newline characters) until it finds
the first character that is not white space. Note, if you declare a variable as a float
rather than a double
, the format specifier is "%f". The l (ell) in "%lf" indicates that the expected variable is a long float. scanf
expects the first non-white space to be an
integer (or a plus or minus sign). quarts
) may not be changed.