Exercise 1: First C and C++

If you are new to Unix then I suggest you read Unix tutorial for beginners

(1) Hello, World!

This exercise will introduce writing & compiling the simplest of C and C++

Create a new directory called ‘hello’

%mkdir hello

%cd hello

Use an editor of your choice (vi, asedit, xemacs,….) to create a new file and type each of the following functions.

If you dont have an editor of choice try typing

    % asedit &
    % nedit &

(i) C version – writing output using C print function

Type the following in file ‘hello.c’:

#include <stdio.h>

int main()


printf("Hello world in C!\n");


Now create a makefile called ‘makefile_c’ as follows (make sure you use <TAB> to indent lines):

# simple C makefile

CC = gcc

hello: hello.o

$(CC) hello.o -o hello

hello.o: hello.c

$(CC) -c hello.c

Now compile and run your C program:

%make -f makefile_c


This should produce the response:

‘Hello world in C!’


(ii) C++ version – writing output using C++ stream class

Type in file ‘hello.cc’:

#include <iostream>

using namespace std;

int main()


cout << "Hello, World in C++\n";


and create a makefile ‘makefile_c++’

# simple C++ makefile

CC = g++

helloc++: hello.o

$(CC) hello.o -o helloc++

hello.o: hello.cc

$(CC) -c hello.cc

Now compile & run as with the C++ version:

%make -f makefile_c++




Notes on Compilation 


In the above examples you have used makefiles to compile your source code. Unix provides this facility for compiling and linking source files. The basic syntax of the makefile is:

"target file": "files it depends on"

<TAB> "what to do to make target"


Compiling C/C++ Programs

The basic scheme for compilation of C & C++ programs consists of a compiler and linker.

First source files *.c and header files *.h are compiled to create a machine dependent binary object file *.o:

file.o: file.c

$(CC) -c file.c

which produces the command

% gcc -c file.c

and creates object file.o

Secondly, the object files are linked with any required C/C++ libraries to produce the machine dependent binary executable:

executable: file.o

$(CC) file.o -o executable

produces the following command and creates executable:

%gcc file.o -o executable

(2) Procedural Programming – C with functions 

This exercise illustrates how C is commonly used to implement a set of functions for some data – procedural design. In this case the data is the radius of a circle which is input by the user. Functions are called to evaluate the area and circumference.

To modularise the program the functions are implement in a separate file ‘func.c’ from the main program ‘proc.c’. The function definition is specified in file ‘func.h’ to allow both ‘func.c’ and ‘proc.c’ to use the same function definition.

Type in the following 3 files:


#include <stdio.h> /* <> for system header file */

#include "func.h" /* "" for user header file */

int main()


double r,c,a;

printf("Please enter the radius? ");



printf("Circumference: %lf\n",c);


printf("Area: %lf\n",a);



#include "func.h"

double circum(double radius)

{ return 2.0*3.14159*radius; }

double area(double radius)

{ return 3.14159*radius*radius; }



 double circum(double radius);

/* Given the radius, returns the circumference */

double area(double radius);

/* Given the radius, return the area */


Now type the makefile:



circle: proc.o func.o

$(CC) proc.o func.o -o circle 

proc.o: proc.c func.h

$(CC) -c proc.c

func.o: func.c func.h

$(CC) -c func.c

Compile and link:

% make -f makefile_circle

Run the program:

% circle

Please enter the radius? 0.5

The circumference is 3.141590


3.     Object-Oriented Programming – C++ classes

In this exercise we rewrite the procedural program of (2) in an object oriented form. In this simple case the object is a circle with data ‘radius’ and operations to evaluate cirumference and area. In C++ objects are classes in this case ‘CircleC’ defined in ‘circle.cc’ and ‘circle.hh’.

Type the following files, make and run the program.


#include <iostream>

using namespace std;

#include "circle.hh"

int main()


cout << "Please enter the radius?\n";

double r;

cin >> r;

CircleC circle(r); // construct a circle radius r

cout << "Area: " << circle.Area() << "\n";

cout << "Circumference: " << circle.Circum() << "\n";



// C++ header file for CircleC

class CircleC {


CircleC(double radius); // Constructor for CircleC

double Circum();// Member function - circumference

double Area(); // member function - area


double radius;// private data cannot be accessed outside


Note: make sure you but a semi-colon ";" at the end of the class declaration


// C++ implementation

#include "circle.hh"

CircleC::CircleC(double r) : radius(r) {}

double CircleC::Circum() { return 2.0*3.14159*radius;}

double CircleC::Area() { return 3.14159*radius*radius;}



circlec++: object.o circle.o

$(CC) object.o circle.o -o circlec++ 

object.o: object.cc circle.hh

$(CC) -c object.cc

circle.o: circle.cc circle.hh

$(CC) -c circle.cc

Now make and run this example.


(4) Optional Additional Exercises in C

These exercises provide further background on C:

(a)The qualifier const means that a variable cannot be changed. Try to compile and run this program:

const int a=0;



(b) C functions can be called recursively. Write a function to compute the factorial of a number supplied by the user. [Use scanf("%d",&n);] 

(c) The C preprocessor allows "macros".

#define PI 3.1415926

c = 3 * PI;

PI is replaced by the number 3.1415926 before compilation. Rewrite (2) to use this macro. In which file and where should it go?