Up until now all the variables we have declared have been local to a block, either the body of the main function or a block within it. C also lets you declare another flavour of variable, which is called global.

Global variables are declared outside any blocks. The exist for the entire duration of the program.

In the program loaded into the Virtual CPIC we have two variables. The variable maximum is not inside any block, and is therefore global. We note that we can see it, and its value, before we have run the program. The compiler will create space for global variables and set their values before the program starts to run. We say that their values are set at compile time.

When the program runs we find that the variable count is created and set to a value. This happens as the program runs so we say that local variables are created at run time. You use local and global variables in exactly the same way.

Of course if you create a local variable with the same name as an existing global variable the global variable is scoped out while the local variable exists.

Global variables are special, in that any part of the program can see and use them. For that reason we use them to hold information which is to be available to all. In the example above I have called my global variable maximum. This may be because that variable holds the maximum number of sheep I am allowed to count.

One of the design decisions which you need to make is the variables which are to be local, and which are to be global. All of your fellow programmers will need to agree on the global variables and what they mean, so that your program can fit together properly.