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:
quarts.c: original version
/* 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; }
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.
Awkwardness of quarts.c
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
- change the program line quarts = 2;
- re-compile the program
- re-run the program.
quarts.c: revised to get quarts-rev.c
To resolve the awkwardness found in quarts.c, we would like the program, when run, 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.
The resulting program is quarts-rev.c
Sample interaction
If the user enters 23 as the value of quarts in a revised program, the full interaction within a terminal window might be:
This program converts quarts to liters Enter number of quarts (an integer): 23 23 quarts = 21.765669 liters
Handling user input for simple values (e.g., type int or double)
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 background, the following discussion will provide enough discussion for our needs at the start of this course. Expanded features of scanf are discussed in a later module, after readers have an understanding of several additional concepts and elements of C.
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:
- scanf identifies the function to be used.
- "%d" indicates that the function should read an integer.
- &quarts indicates that the integer should be placed in the quarts variable. (The ampersand & indicates "the location of". The & symbol is discussed at length later in this course!).
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.
printf ("Enter number of quarts (an integer): ");
The full, revised program, quarts-rev.c, follows:
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 |
/* 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
-
Both scanf and printf are defined in the stdio library, so the same include statement covers both functions.
-
The text for the "Enter" string does not contain the newline character "\n". When the program runs, the computer prints:
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.)
-
Since variable 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.
-
When reading input with "%d" or "%lf" format, scanf skips over any initial white space (e.g., spaces, tabs, newline characters) until it finds the first character that is not white space.
-
With "%d" format, scanf expects the first non-white space to be an integer (or a plus or minus sign).
- If the first non-white space character is not a digit (or a plus or minus sign), reading will fail, and the original value of the variable (e.g. quarts) may not be changed.
- If the first non-white character is a digit (or a plus or minus sign), reading continues digit-by-digit, assuming the number is given in a decimal format. Reading stops after the first non-digit.
Example 2: quarts-gallons.c
As a second example, consider computing the liter equivalent for a number of gallons and quarts. In this context, a user might be asked for two values. In practice, this might be accomplished in either one or two scanf statements.
Two scanf statements:
printf ("Enter integers for gallons and quarts: "); scanf ("%d", &gallons); scanf ("%d", &quarts);
One scanf statement:
printf ("Enter integers for gallons and quarts: "); scanf ("%d %d", &gallons, &quarts);
The full program, quarts-gallons.c, follows:
/* A program to convert gallons and quarts to liters */ #include <stdio.h> int main () { printf ("This program converts gallons and quarts to liters\n"); int gallons, quarts; double liters; printf ("Enter integers for gallons and quarts: "); scanf ("%d %d", &gallons, &quarts); liters = (4*gallons + quarts) / 1.056710 ; printf ("%d gallons, %d quarts = %lf liters\n", gallons, quarts, liters); return 0; }
Notes for quarts-gallons.c
-
Declaring two int variables may be done in either one or two statements:
Two declaration statements:
int gallons; int quarts;
One declaration statement:
int gallons, quarts;
-
To enter two integers, leave at least some white space (space character, tab, or newline character) between them, so the computer knows when one integer is complete and the next will start.
-
Since 1 gallon is equivalent to 4 quarts, the computation in the program converts the total number of gallons and quarts to a total quarts and then converts the result to liters.