Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help
next up previous contents
Next: Compilation Stages Up: ANSI C for Programmers Previous: List of Figures   Contents


C's popularity has increased as Unix has become more widespread. It is a flexible, concise and small language, with a mix of low-level assembler-style commands and high-level commands. It's much used with the ${\cal X}$ graphics system and increasingly for numerical analysis. The first de facto standard C was as described in [7] and is often known as K&R C . The current standard is ANSI C [12] in which the source contained in this document is written. Check your local documentation to see how to compile the code. In this documentation cc -Aa will be used.

To those who have programmed before, simple C programs shouldn't be too hard to read. Suppose you call this program basics.c

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

int mean(int a,int b)
  return (a + b)/2;

int main()
int i, j;
int answer;
   /* comments are done like this */
   i = 7;
   j = 9;

   answer = mean(i,j);
   printf("The mean of %d and %d is %d\n", i, j, answer);
   exit (0);

Note that the source is free-format and case matters.

All C programs need a main function where execution begins. In this example some variables local to main are created and assigned (using `=' rather than `:='. Also note that `;' is a statement terminator rather than a separator as it is in Pascal). Then a function mean is called that calculates the mean of the arguments given it. The types of the formal parameters of the function (in this case a and b) should be compatible with the actual parameters in the call. The initial values of a and b are copied from the variables mentioned in the call (i and j).

The function mean returns the answer (an integer, hence the `int' before the function name), which is printed out using printf. The on-line manual page describes printf fully. For now, just note that the 1st argument to printf is a string in which is embedded format strings; %d for integers, %f for reals and %s for strings. The variables that these format strings refer to are added to the argument list of printf. The `\n' character causes a carriage return.

C programs stop when

This program can be compiled using `cc -Aa -o basics basics.c'. The `-o' option renames the resulting file basics rather than the default a.out. Run it by typing `basics'. A common mistake that beginners make is to call their executable `test'. Typing test is likely to run the test facility built into the shell, producing no input, rather than the user's program. This can be circumvented by typing ./test but one might just as well avoid program names that might be names of unix facilities. If you're using the ksh shell then typing `whence program_name' will tell you whether there's already a facility with that name.

next up previous contents
Next: Compilation Stages Up: ANSI C for Programmers Previous: List of Figures   Contents
Tim Love 2010-04-27