The Form of a C ProgramĀ 


C Programming TutorialĀ 

Go to the Table of Contents

Vist the Gifcom Corporation

What goes into a C program? What will it look like?

C is made up entirely of building blocks which have a particular `shape' or form. The form is the same everywhere in a program, whether it is the form of the main program or of a subroutine. A program is made up of functions, functions are made up of statements and declarations surrounded by curly braces { }.

The basic building block in a C program is the function. Every C program is a collection of one or more functions, written in some arbitrary order. One and only one of these functions in the program must have the name main(). This function is always the starting point of a C program, so the simplest C program would be just a single function definition:

main ()

{
}

The parentheses () which follow the name of the function must be included even though they apparently serve no purpose at this stage. This is how C distinguishes functions from ordinary variables.

The function main() does not have to be at the top of a program so a C program does not necessarily start at line 1. It always starts where main() is. Also, the function main() cannot be called from any other function in the program. Only the operating system can call the function main(): this is how a C program is started.

The next most simple C program is perhaps a program which calls a function do_nothing and then ends.

/******************************************************/
/* */
/* Program : do nothing */
/* */
/******************************************************/

main() /* Main program */

{
do_nothing();
}

/******************************************************/

do_nothing() /* Function called */

{
}

The program now consists of two functions, one of which is called by the other. There are several new things to notice about this program. Firstly the function do_nothing() is called by typing its name followed by the characteristic () brackets and a semi-colon. This is all that is required to transfer control to the new function. In some languages, words like CALL or PROC are used, or even a symbol like &. No such thing is needed in C. The semi-colon is vital however. All instructions in C must end with a semi-colon. This is a signal to inform the compiler that the end of a statement has been reached and that anything which follows is meant to be a part of another statement. This helps the compiler diagnose errors.

The `brace' characters { and } mark out a block into which instructions are written. When the program meets the closing brace } it then transfers back to main() where it meets another } brace and the program ends. This is the simplest way in which control flows between functions in C. All functions have the same status as far as a program is concerned. The function main() is treated just as any other function. When a program is compiled, each function is compiled as a separate entity and then at the end the linker phase in the compiler attempts to sew them all together.

The examples above are obviously very simple but they illustrate how control flows in a C program. Here are some more basic elements which we shall cover.

  • comments
  • preprocessor commands
  • functions
  • declarations
  • variables
  • statements

The skeleton plan of a program, shown below, helps to show how the elements of a C program relate. The following chapters will then expand upon this as a kind of basic plan.

/****************************************************/
/* */
/* Skeleton program plan */
/* */
/****************************************************/

#include <stdio.h> /* Preprocessor defns */
#include <myfile.c>

#define SCREAM "arghhhhh"
#define NUMBER_OF_BONES 123

/****************************************************/

main () /* Main program & start */

{ int a,b; /* declaration */

a=random();
b=function1();
function2(a,b);
}

/****************************************************/

function1 () /* Purpose */

{
....
}

/****************************************************/

function2 (a,b) /* Purpose */

int a,b;

{
....
}