Editing, compiling and running C programs


Exercise 0

Create subdirectory (i.e., folder) comp411lab in your home directory.

        % cd ~
        % mkdir comp411lab

You must protect this folder so others cannot see inside it. You use the filesystem command (fs) to set up an access control list (ACL) that allows nobody any access (-clear) except you (all access). Be sure to replace yourloginname with your actual login id.

        % fs setacl -clear -dir comp411lab -acl yourloginname all

Inside it, create a subfolder lab2 and make it your current working directory.

Download/copy the sample program intro.c into the lab2 subfolder. You can copy it using the following command:

        % cp /home/montek/public_html/teaching/Comp411-Fall12/Labs/lab2files/intro.c . 

Alternatively, you can use the wget command as described in Lab 1.

For this tutorial, you will edit files using the editor pico. View the C file using using pico as follows:

        % pico intro.c

This editor is text-based, and you will need to use Control-key combinations to navigate the menu. Quit the editor, and enter the following Unix command to compile the program:

        % gcc intro.c

What do you think has happened? Have a look at the contents of your current working directory. Find out what type of data is contained in the file a.out (do you still remember how to do this?). Try to display the contents of this file or to open it with the editor.

Execute the program by typing

        % ./a.out

and see what happens. Do you understand the C text of the program?

NOTE: always put a "./" before the program name when executing a program in your working directory.

Remove the file a.out and try compiling the program in a slightly different way:

        % gcc intro.c -o intro

What is the difference? Can you run the program now? Run the program again, but this time redirect its output into the file intro.out (using ">" to redirect its output, as described in Lab 1). What has happened to the prompting message? Have a look into the file intro.out and make sure that you understand what is there and why. Create file intro.in with a value of the radius and run the program in such a way that it takes its input from intro.in and writes the output into intro.out.


Exercise 1

Modify the program so that it takes the radius value as input in centimeters but displays the result in square inches. (One inch is 2.54 centimeters exactly.) Use pico to edit the file to make this modification, and save the file as ex1.c inside your lab2 folder.

Compile this program and run it one a couple of different inputs to verify that it is working correctly. The inputs and outputs can be from the keyboard/terminal; you do not need to use redirection of input/output from/to files for this exercise.


Exercise 2

Copy the file ex1.c to ex2.c (so you do not accidentally edit ex1.c any further). Modify the program in ex2.c to make it work on multiple inputs. In particular, it should repeatedly ask for a radius value (in centimeters), and print the corresponding area (in square inches), until the user enters the value 0 as radius. At that point, the program should print the area, and then terminate.

Compile this program and run it, feeding it several radius values from the keyboard, and verifying that the area values it displays are correct. A single run of the program should be able to compute as many of these radius-to-area calculations as you want, until you enter 0.


Revised: 25 September 2006, r.webb@surrey.ac.uk
Modified: 31 August 2012, Montek Singh, montek@cs.unc.edu