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:

Getting Started with Programming in C

A program in the C programming language (or other computer languages) indicates a sequence of detailed instructions that specify the steps to be followed to solve a problem.


Programs provide a mechanism for communicating algorithms

Since a program articulates a proposed algorithm to accomplish a desired task, the program should be considered as a formal mechanism for precise communication. As a communication vehicle, a program has at least three audiences:

Throughout this course, we will write code that addresses the needs of all three audiences. Code with a specific syntax can be utilized by a computer, but code written just for a machine often misses the point.

Examples First

As you begin to program in C, you will encounter a reasonably large number of mechanics and details. If you have not programmed before, the steps may seem unintuitive and awkward. Further, programs in C require a specific syntax or format. Since the C environment may be new and different, it may take some time to become comfortable with the syntax (format) and process.

Don't panic, before long (perhaps a few days), many of these mechanics will become familiar, and you will be able to focus on more conceptual elements of problem solving with C.

Since the various rules and procedures may be unfamiliar, this module highlights working examples. Readings, such as this one, will involve some complete programs, and commentary will explain beginning elements of C and relevant procedures. Later modules will provide more complete discussions of imperative problem solving and the C programming language.


Example 1: quarts.c — A first program in C

As a first program in C, consider the following code that converts a number of quarts to liters. Commentary in the right column, next to the program, explains each program element.

Numerous blank lines are inserted for spacing, so the commentary generally is near the lines discussed. C allows white space (blank lines and spaces) to be inserted many places for readability. (White space may not be placed in the middle of numbers or names, but almost anywhere else.) The program, without extra blank lines, is available as the file quarts.c

A subsequent section explains how this program may be run.


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

Commentary for quarts.c


#include <stdio.h>  // use C's standard I/O library

int main ()         // beginning of main program

{

    printf ("Program to convert quarts to liters\n");  
                    // write opening statement

    /* declaration of variables, with their types */
    int quarts;      /* int means integer (no decimal point) */
    double liters;   /* double means real number */

    quarts = 2;     // specify number of quarts as 2

    liters = quarts / 1.056710; 
                   // arithmetic and assignment

    printf ("%d quarts = %lf liters\n", quarts, liters);
                   // write text and new line

   return 0;    // the program ran without errors
}

Developing and Running a C Program

Creating, refining, and running a C program requires three basic steps:

  1. Editing: Use a simple text editor to create and edit a C program.
    Sample command for program quarts.c (all typed within a terminal window):

       aquamacs quarts.c &
    

Notes on editing, compiling, and running a C program


  1. Compiling: Use a compiler to translate the C program to a machine language version.
    Sample command using gcc to produce machine-language program quarts from quarts.c

       gcc -o quarts quarts.c
    

  1. Running: Type the name of the program, perhaps preceded by ./ to indicate the program is located in the current directory.
    Sample command for quarts

       ./quarts
    
As with any command in a terminal window, the prefix ./ indicates that this program may be found in your current directory.

Putting the steps together

The following interaction in a terminal window shows a sample session, run on workstation perlis, including writing, translating, and running quarts.c.

     perlis$ aquamacs quarts.c &
     perlis$ gcc -o quarts quarts.c
     perlis$ ./quarts
     This program converts quarts to liters
     2 quarts = 1.892667 liters

This example uses the emacs editor to create and edit the file quarts.c, the gcc compiler to create the machine language version of the program, and the name of the program to run the program in a terminal window. This example also shows the output created by the program.


Example 2: temperature.c — Converting Fahrenheit to Celsius

Our second C program addresses the problem of converting a temperature in Fahrenheit to the corresponding temperature in Celsius. The solution, program temperature.c, utilizes many of the elements of the first example, quarts.c, reinforcing earlier comments, although a few elements are different.

/* program to convert a temperature in fahrenheit
   to a temperature in celsius */

#include <stdio.h>

int main ()
{
  int fahrenheit;
  double celsius;

Notes for temperature.c


  fahrenheit = 72;
  celsius = (fahrenheit - 32.0) * 5.0 / 9.0;

  printf (" %d degrees fahrenheit = %5.2lf degrees celsius\n", 
          fahrenheit, celsius);

  return 0;

}

Compiling and running temperature.c

Following the same process used for quarts.c, the program temperature.c could be compiled and run on workstation perlis as follows:

   perlis$ gcc -o temperature temperature.c
   perlis$ ./temperature
     72 degrees fahrenheit = 22.22 degrees celsius

Example 3: temperature-acg.c — Temperature Conversion with Averaging

As another example, consider the problem of writing a program that starts with two temperatures in Fahrenheit, computes the average of the temperatures, determines the corresponding Celsius temperatures, and prints the result in a table. For example, one run of the program might yield:

                     Fahrenheit  Celsius
    Temperature 1:      86.1       30.1
    Temperature 2:     111.1       43.9
          Average:      98.6       37.0

The following program temperature-avg.c addresses this problem, building on the two earlier programs (above).


/* program to convert two temperatures in fahrenheit and
   their average to temperatures in celsius */

#include <stdio.h>

int main ()
{
  /* declare variables */
  /* the fahrenheit and celsius temperatures may
     contain decimal points, so will be real numbers
     (e.g., doubles) */
  double fahr1, fahr2;
  double cel1, cel2;  

  /* initialization */
  fahr1 =  86.1;
  fahr2 = 111.1;

Notes for temperature-avg.c


  /* compute celsius equivalents */  
  cel1 = (fahr1 - 32.0) * 5.0 / 9.0;
  cel2 = (fahr2 - 32.0) * 5.0 / 9.0;

  /* computer average values */
  double sum = fahr1 + fahr2;  /* average computation split into
                                  two steps for variety */
  double fahravg = sum / 2.0;
  double celavg = (fahravg - 32.0) * 5.0 / 9.0;
  
  /* print table of results */

  printf ("                 Fahrenheit  Celsius\n");  //table title
  /* report results to 1 decimal place */
  printf ("Temperature 1:   %7.1lf %10.1lf\n", fahr1, cel1);
  printf ("Temperature 2:   %7.1lf %10.1lf\n", fahr2, cel2);
  printf ("      Average:   %7.1lf %10.1lf\n", fahravg, celavg);
  
  /* tell operating system "no errors" */
  return 0;
}


created created 18 July 2011
reformatted and revised 8 May 2016
typos corrected 28 August 2016
modest editing, reformatting 22 August 2017
Example 3 added 6 September 2017
use of Aquamacs 22 January 2018, 16 January 2022
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.