Example Program

The following C source program consists of two source files. It gives an overview of some of the various declarations and definitions possible in a C program. Later sections in this book describe how to write these declarations, definitions, and initializations, and how to use C keywords such as static and extern. The printf function is declared in the C header file STDIO.H.

The main and max functions are assumed to be in separate files, and execution of the program begins with the main function. No explicit user functions are executed before main.

/*****************************************************************
                    FILE1.C - main function
*****************************************************************/

#define ONE     1
#define TWO     2
#define THREE   3
#include <stdio.h>

int a = 1;                       /* Defining declarations     */
int b = 2;                       /*  of external variables    */

extern int max( int a, int b );  /* Function prototype        */

int main()                       /* Function definition       */
{                                /*  for main function        */
    int c;                       /* Definitions for           */
    int d;                       /*  two uninitialized        */
                                 /*  local variables          */

    extern int u;                /* Referencing declaration   */
                                 /*  of external variable     */
                                 /*  defined elsewhere        */
    static int v;                /* Definition of variable    */
                                 /*  with continuous lifetime */

    int w = ONE, x = TWO, y = THREE;
    int z = 0;
    z = max( x, y );             /* Executable statements     */
    w = max( z, w );
    printf( "%d %d\n", z, w );
    return 0;
}

/****************************************************************
            FILE2.C - definition of max function
****************************************************************/

int max( int a, int b )          /* Note formal parameters are   */
                                 /*  included in function header */
{
    if( a > b )
        return( a );
    else
        return( b );
}

FILE1.C contains the prototype for the max function. This kind of declaration is sometimes called a “forward declaration” because the function is declared before it is used. The definition for the main function includes calls to max.

The lines beginning with #define are preprocessor directives. These directives tell the preprocessor to replace the identifiers ONE, TWO, and THREE with the numbers 1, 2, and 3, respectively, throughout FILE1.C. However, the directives do not apply to FILE2.C, which is compiled separately and then linked with FILE1.C. The line beginning with #include tells the compiler to include the file STDIO.H, which contains the prototype for the printf function. Preprocessor directives are explained in the Preprocessor Reference.

FILE1.C uses defining declarations to initialize the global variables a and b. The local variables c and d are declared but not initialized. Storage is allocated for all these variables. The static and external variables, u and v, are automatically initialized to 0. Therefore only a, b, u, and v contain meaningful values when declared because they are initialized, either explicitly or implicitly. FILE2.C contains the function definition for max. This definition satisfies the calls to max in FILE1.C.

The lifetime and visibility of identifiers are discussed in Lifetime, Scope, Visibility, and Linkage. For more information on functions, see Chapter 6, Functions.