1 Introduction

C is a general-purpose programming language designed in about 1972 for use on the Unix operating system. The decisions to make the language portable and use it to rewrite Unix have caused it to become very popular, and many operating systems, compilers and general purpose applications have been coded using C. The C programming language by Brian Kernighan and Dennis Ritchie (second edition) is a good introductory textbook.

C combines the advantages of high-level languages such as Fortran in the realm of control structures and portability with ‘machine-level’ features such as pointers, increment operators, bitfields and bitwise operators. The language is especially suited to system programming because its data types and operators are closely related to the operations of most computers. However Starlink does not recommend the use of C for applications programming. There are several reasons for this, not least that Fortran provides better facilities for the manipulation of multi-dimensional data arrays and the dynamic declaration of array size. Additionally there is the problem of maintenance. It is very easy (and indeed usual) to write C code which is cryptic; and there is an ever-present temptation to write highly-condensed code. A common failing of C programmers is to write code and then collapse it into the shortest possible form assuming that anyone reading the code will be an expert. Whilst the experienced C programmer will recognise the patterns of code which result, it is recommended by Starlink that such structures are well commented for the benefit of the less experienced.

The language has no facilities to perform input and output, and lacks Fortran’s large set of intrinsic functions. However all implementations are accompanied by a (more or less) standard library of functions to accomplish I/O and other tasks. These standard functions are made available to a program by specifying the modules which contain the required set of functions via an include file. For example the Standard I/O routines are contained in the file stdio.h, which must be explicitly included in the program source if the functions contained therein are to be used.

The suggestions which follow are made with maintainability, portability and efficiency in mind. Several of these recommendations are made because certain code constructs will have different effects on different C implementations. In some cases the ambiguity is resolved by the ANSI standard. However it may be some time before all C implementations meet the standard, and in any case it is preferable to avoid code which suggests more than one possibility to the human reader.

Starlink has adopted The Elements of C Programming Style by Jay Ranade and Alan Nash as its principal C programming standard. This book is essential reading for all contributors of Starlink programs written in C. The present document does not reiterate what is in the book, but contains some additions and clarifications. Where the advice in Chapter 18, Coding for Non-C Programmers, diverges from that in the main body of the book, Starlink recommends that you follow Chapter 18.