CSC 161.01 Grinnell College Fall, 2019
Scribbler 2
CSC 161.01
Imperative Problem Solving with Lab
Scribbler 2

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 after the program explains each element of this program. A subsequent section explains how this program may be run.


/* A simple program to convert a number of quarts to liters
   Version 1:  global variables only
*/

#include <stdio.h>                  /* reference to standard I/O library */

int main ()                         /* beginning of main program */
{
   printf ("This program converts a number of 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 the 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 */
}

Commentary on the quarts.c program

Developing and Running a C Program

To prepare a program in C, one uses a text editor, such as emacs or vi. Do not use a word processing package, such as Word, OpenOffice, or LibreOffice, because these packages insert extensive formatting and font information that will confuse the computer as it tries to follow the instructions in your program.

Once the program has been written with the editor and saved, the program must be translated from C to machine language. (In Scheme, such work is done behind the scenes, but in C the translation must be made explicit.) The translation is called compiling, and the utility used to compile a program is called a compiler. A common compiler for C is called gcc.

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

     perlis$ emacs quarts.c &
     perlis$ gcc -o quarts quarts.c
     perlis$ ./quarts
     This program converts a number of quarts to liters
     2 quarts = 1.892667 liters

In this example, emacs quarts.c & starts the emacs editor to prepare a new program called quarts.c. The ampersand (&) allows editing to take place while the terminal window can still be used for other activities.

To compile the program, the line gcc -o quarts quarts.c uses the gcc with the program we have edited (quarts.c). The directive -o quarts indicates we want the compiler to place our translated program in an output file called quarts.
Warning: Be sure the name of the output file (quarts) is different from the name of your original program (quarts.c). If you use the same name for both, your original program will be overwritten and lost!

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


This document is available on the World Wide Web as

http://www.walker.cs.grinnell.edu/courses/161.fa11/modules/module-getting-started/quarts-annotated.shtml

created created 18 July 2011
last revised 18 July 2011
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.

ccbyncsa.png

Copyright © 2011-2016 by Henry M. Walker.
Selected materials copyright by Marge Coahran, Samuel A. Rebelsky, John David Stone, and Henry Walker and used by permission.
This page, and other materials developed for this course, are under development.
All readings and reference materials for this course are licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.