CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

Providing our variable definitions

In the C file globals.c we finally define the global variables.

It is here that the compiler allocates memory space for them.

In particular, we now define the size of the projmarks and exammarks arrays, in a manner dependent on the preprocessor constants from calcmarks.h
This allows us to provide all configuration information in one (or more) header files. Other people modifying your programs, in years to come, will know to look in the header file(s) to adjust the constraints of your program.


#include  "calcmarks.h"              // we use double-quotes

double    projmarks[ MAXMARKS ];     // array's size is defined  
double    exammarks[ MAXMARKS ];     // array's size is defined  

bool      verbose = false;           // global is initialized

Global variables are automatically 'cleared'

By default, global variables are initialized by filling them with zero-byte patterns.
This is convenient (of course, it's by design) because the zero-byte pattern sets the variables (scalars and arrays) to:

  • 0 (for ints),
  • '\0' (for chars),
  • 0.0 (for floats and doubles),
  • false (for bools), and
  • zeroes (for pointers).

Note that we could have omitted the initialisation of verbose to false, but providing an explicit initialisation is much clearer.

 


CITS2002 Systems Programming, Lecture 17, p6, 26th September 2023.