Prev - Index - Next

C and C++

... one of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs.

Robert Firth


In 1969, the first version of Unix climbed out of the primordial swamp at the AT&T labs, having self-assembled itself from PDP-7 assembler. Around 1971, C evolved out of a series of earlier languages, including B (no-one has ever accused software engineers of creative naming). In 1973, Unix was rewritten in C (with a small amount of assembler), and the rest is history.

C has spawned a number of descendants, including Objective-C, C++ (also developed at the AT&T labs) and Java.

The vast majority of the code used in the Unix & Linux kernel, support tools, C compiler, shells, X-Windows etc. is written in C and, to a lesser extent, C++. Thus, C is a very useful language to know.

The good, the bad and the ugly

Historically, C has been popular because:

However, C also has a number of drawbacks:

If you want to learn C, there are a number of good C books, including the original text (referred to as K&R) by Kernighan and Ritchie.

David Marshal, from Cardiff University, has an excellent on-line C tutorial.

Compiling C code on Linux

There are a number of C (and C++) compilers available for Linux, both free and commercial. Of these, the most widely used compiler is GNU C. The GNU C compiler offers support for C, C++ and Objective C, and can be installed as a cross-platform compiler, allowing you to develop code for other systems on your computer.

A typical compilation command for a simple (one file) C project would be:

        gcc -Wall -pedantic -ansi -g -o foobar foo.c

-Wall turns on all warnings, -pedantic activates a 'pedantic' level of warnings, -ansi turns on checks for conformance with the ANSI C standard, -g leaves debugging information inside the binary (which can be removed with the strip command) and -o foobar gives the output executable the name foobar.

If you do not use the -o flag, then the resulting executable will be called a.out, and remember that unless the current directory is in your path, you will need to run any executable with a preceeding ./; for example:


Some sample code & error messages

This is a program that takes a circle's diameter as a command line argument and calculates the area of the circle.

We have compiled it with the line:

        gcc -Wall -pedantic -o areacalc sample.c

Note that we are using the M_PI #define from math.h. If we were using any of the math functions, we would need to include the math library when compiling, like this:

        gcc -Wall -pedantic -o areacalc sample.c -lm

/* Calculates a circle's area
 * Demonstration C program for the March 1999 LUV talk
 * Written by Graeme Cross

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

main(int argc, char *argv[]) {

  float diam, radius, area;
  printf("\nArea calculator\n\n");

  if (argc != 2) {
    fprintf(stderr, "Error - Syntax: areacalc diameter\n\n");

  diam = atof(argv[1]);                      /* this needs error checking */
  raduis = diam/2.0;
  area = radius * radius * M_PI;

  printf("Circle's diameter = %f\n", diam);
  printf("-> Area = %0.4f\n\n", area);

/* eof */

Compiling this reveals an error on line 23, where a variable has been incorrectly spelt.

        [/usr/home/graeme/work/LUV/prog]$ gcc -o areacalc areacalc.c
        areacalc.c: In function `main':
        areacalc.c:23: `raduis' undeclared (first use this function)
        areacalc.c:23: (Each undeclared identifier is reported only once
        areacalc.c:23: for each function it appears in.)

Any good text editor can automatically compile the code for you, and step through the code highlighting each error and warning generated by the compiler.

Note that the C compiler is only capable of catching the most basic errors, such as syntax errors. It does not detect drastic errors such as memory leakage or division by zero.

For the record, here is the corrected line!

        radius = diam/2.0;


Linux supports a number of free and commercial C++ compilers, with the split streams of the GNU and EGCS compilers being the dominant compilers. These compilers come with standard libraries and a fairly robust implementation of the STL (the Standard Template Library).

As of late 1998, C++ is defined by an ISO standard, which is good news for implementors and developers alike.

C++ developers shifting from the Mac or MS Windows worlds will probably miss the "visual" C++ development tools, although there are some basic class browsers available for free, and a number of excellent commercial C++ support tools, such as OO-Browser for EMACS.

Prev - Index - Next