CITS2002 Systems Programming  
next CITS2002 CITS2002 schedule  

The structure of C programs

Let's look at the high-level structure of a short C program, rotate.c (we're using ellipsis to omit some statements, for now).
At this stage it's not important what the program is supposed to do.

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

/* Compile this program with:
   cc -std=c11 -Wall -Werror -o rotate rotate.c
 */

#define ROT 13

static char rotate(char c)
{
    c = c + ROT;
    .....
    return c;
}

int main(int argc, char *argv[])
{
    // check the number of arguments
    if(argc != 2) {
        ....
        exit(EXIT_FAILURE);
    }
    else {
        ....
        exit(EXIT_SUCCESS);
    }
    return 0;
}

Of note in this example:

  • Characters such as a space, tab, or newline, may appear almost anywhere - they are stripped out and ignored by the C compiler.

    We use such whitespace characters to provide a layout to our programs. While the exact layout is not important, using a consistent layout is very good practice.

  • Keywords, in bold, mean very specific things to the C compiler.

  • Lines commencing with a '#' in blue are processed by a separate program, named the C preprocessor.

    In practice, our program is provided as input to the preprocessor, and the preprocessor's output is given to the C compiler.

  • Lines in green are comments. They are ignored by the C compiler, and may contain (almost) any characters.

    C11 provides two types of comments -

    1. /* block comments */  and
    2. // comments to the end of a line

 


CITS2002 Systems Programming, Lecture 2, p1, 24th July 2024.