Editing, compiling and running C programs


Note you may copy all the files for this tutorial from /home/gb/public/lab2files or you may download them using wget and the url in the tutorial.

Exercise 1

Create subdirectory comp411lab in your home directory. Protect it from prying eyes using the command

        % fs sa comp411lab yourloginname all -clear

Inside it, create further subdirectory lab2 and make it your current working directory.

For this tutorial, you will edit files using the editor pico.

        % pico filename

Download/copy the sample program intro.c, and view it using pico. Ask for assistance if you have problems with doing this!

Now 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. 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. Now place 3 different values of the radius (separated by spaces) into intro.in, run the program the same way as before and see how many results get written into intro.out. Explain what has happened.


Exercise 2

Remove the closing symbol } from the C text of the program intro.c and compile it again. Explain the result. Now restore the removed symbol and replace identifier main with main_program. Can you compile and run the program now? At what stage do things go wrong and why?

Restore the correct name of the main function. Modify the program so that it works in centimeters rather than in inches. Now make it take the radius value in centimeters but display the result in square inches. One inch is 2.54 centimeters.


Exercise 3

Compile the program sqrt.c using

        % gcc sqrt.c -o sqrt

Note the error message you receive. Now compile it using

        % gcc sqrt.c -o sqrt -lm

This links in the math library in which the sqrt function is defined. Run the program and input a few different numbers. What happens when the input number is negative?


Exercise 4

Compile and run the following programs. Place all the files into your directory lab2. Check that you understand the purpose and output of each program, and see how much of the program text you understand. Do not hesitate to ask for assistance when you need it!

Don't worry if you don't understand very much of anatomy.c.


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