Science and Computers I -- PHY307/607

C Reference


Overview of C

Programming is the activity of communicating algorithms to computers. An algorithm is a computational procedure whose steps are completely specified. We are used to giving instructions in English and having other people carry them out. The programming procedure is analogous, except that computers have no tolerance for ambiguity and must have all steps specified in tedious detail. So a very precise language is needed - a a programming language. C is one such language, probably one of the most powerful, and certainly the most successful currently available.

A given problem may typically be broken down into a series of smaller tasks. In C these tasks are called functions. A C program is basically just a collection of functions which cooperate together to provide the solution to our original problem. It is often termed a procedural language as opposed to object-oriented languages such as C++ or Java.

Functions

Typically a function must be given data to work on - these are passed to the function through its arguments, for example, it can declared as

 f(int a, double b); 
The meaning of this is that f is a function which operates on two numbers (its arguments) - here an integer a and double precision real number b. Having completed its work a C function can (optionally) return a value to the program that called it - if it returns a double precision number we denote that by declaring the function as

 double f(int a, double b); 
Notice the semi-colon here -- all lines of C which perform some definite action are terminated in semi-colons.

All C programs must contain a main function where execution starts and finishes. This main function will typically set up (or declare) the data structures necessary to the problem at the start of the code and will then call a series of other functions to operate on this data in some way. Typically it is convenient to group all function declarations like the above at the top of the file containing the C program (or source code as it is called). Also at the top are input parameters specified by statements like

#define SOMEPARAMETER 0
In addition, you will commonly see statements like

#include "stdio.h"
These allow the program to call a standard library of other functions (in this case I/O routines).

Input and Output

The functions fprintf, fscanf handle the writing and reading of data to external files. An example of how to use them would be

 (void)fprintf(fp,"%lg $d\n",x,i) 
The first argument of the function fprintf is fp - it identifies a particular file that is to be written to. The text in double quotes tells the function what is to be printed -- a double precision number (%lg) and and integer (%d). There is a single blank between them and then a carriage return is written (\n). The numbers x and i then follow. The prefix (void) instructs the compiler that fprintf does not return a value to the function which called it. The function fscanf is similar - we will not need it here.

Files

To open a file for writing use the following code fragment

fp=fopen("name_of_file","w");
Substituting r for w allows a file to be opened for reading.

Basic Types

The important types for us are: double, int, and FILE* (while the latter is not a fundamental of the language but is defined in "stdio.h" it will be treated that way here). Very useful also is the concept of an array: a collection of data of the same type but labelled by some index. Its easy to create a (1D) array of N double precision numbers called x

 double x[N] 
(N must be defined somewhere with a #define statement) This reserves space in memory for the elements x[0] to x[N-1]. Finally, and a little more obscurely, it is possible to have another data type called a pointer. You can think of this as the address in the computers's memory of a particular piece of data not the data itself. A pointer to an integer would be declared as

 int *p 

Control Structures

For programs to do something useful it is usually necessary to introduce a few more things. First, if we want the program to do the same thing a number of times it is useful to have a short-cut - the for loop. One common place this occurs is for acting identically on all the elements of an array eg.

 for(i=0;i<N;i++)
       x[i]=x[i]*2;
This loop multiplies all the elements of x by two.

So-called if statements can also be useful eg.

if(i>0){ do something }
else {do something else}

Books etc.

There are a billion books on C most of which are acceptable at least as far as giving the correct syntax. One I use which is OK is Practical C Programming a Nutshell book by O'Reilly and Associates Inc.

Example

Click here for some sample C source code
Back to the PHY307 Homepage

This page maintained by Simon Catterall, last updated 21 August, 1997.