The code on the right is a program which declares a variable called count.
When you step through the program you will notice that when the cursor passes over the "int count ;
" line a new item appears in the variable area. This shows that we now have a new variable. The display gives the name of the variable, and the value it currently holds.
When you create a variable it has any old value in it. For some reason our computer had the value 23 lying around in the piece of memory where count is to live, so that is the value which is displayed.
One of the more common programming mistakes is to create a variable and then forget to set it to a sensible value before you use it. If you do this you can look forwards to something different each time the program runs, which might not be a good thing if it is supposed to control a nuclear reactor...
You can have as many variables in your program as you like, depending on what you are trying to do. There are two flavours, local and global. We will look at these a bit later. At the moment we are creating local variables, which must be declared right at the start of a block of code, i.e. immediately after the {
character, this character is known as a 'brace' or 'curly bracket'.
Variable Space
The PICmicro microcontroller we are using limits us to around 300 different variables. This is a very small number if you are used to PC with 512 Mbytes (million bytes) of memory, but as we shall see, for the jobs we are doing the memory available is ample.
/* Simple Variable */
void main ( void )
{
/* create an integer variable */
int count ;
}