CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

The structure of C programs, continued


#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;
}

Same program, but more to note:

  • A variety of brackets are employed, in pairs, to group together items to be considered in the same way. Here:

    • angle brackets enclose a filename in a #include directive,
    • round brackets group items in arithmetic expressions and function calls,
    • square brackets enclose the index when access arrays (vectors and matrices...) of data, and
    • curly brackets group together sequences of one or more statements in C. We term a group of statements a block of statements.

  • Functions in C, may be thought of as a block of statements to which we give a name. In our example, we have two functions - rotate() and main().

  • When our programs are run by the operating system, the operating system always starts our program from main(). Thus, every complete C program requires a main() function.

    The operating system passes some special information to our main() function, command-line arguments, and main() needs a special syntax to receive these.

  • Most C programs you read will name main()'s parameters as argc and argv.

  • When our program finishes its execution, it returns some information to the operating system. Our example here exits by announcing either its failure or success.

 


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