CSC 115.005/006 Sonoma State University Spring 2022
Scribbler 2
CSC 115.005/006:
Programming I
Scribbler 2
Instructor: Henry M. Walker

Lecturer, Sonoma State University
Professor Emeritus of Computer Science and Mathematics, Grinnell College


Course Home References Course Details: Syllabus, Schedule, Deadlines, Topic organization MyroC Documentation Project Scope/
Acknowledgments

Notes:

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


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:

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


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