The scope of a variable refers to what functions can access the variable. A variable defined within a function can only be accessed by that function. The following code demonstrates a variable (j) declared in main. j is local to main and cannot be directly accessed by any other function.
int main(void) {
unsigned char j = 15;
return 1;
}
void externalFunction(void) {
// this function cannot access j
}
A variable that is defined outside of all functions can be accessed by every function in the code, and is referred to as a global variable. Global variables should only be used in situations where it is necessary; otherwise, it is recommended that the scope of variables be limited by keeping them within the function in which they are to be used. The following code demonstrates j defined as a global variable.
unsigned char j = 15;
int main(void) {
// this function can access j
return 1;
}
void externalFunction(void) {
// this function can also access j
}
Subsection14.2.1volatile Variables
When a compiler takes C code and translates it into assembly language, it attempts to optimize that code by leaving out any unused variables and converting unchanging variables to constants, among other optimizations. At times, it may appear that a global variable is unused by functions, especially in the case of an interrupt service routine (ISR).
An ISR is never formally invoked (or called) by the main() function in C, and it may appear to a compiler as if any variables that are used within the ISR are unused (in which case it does not save them in memory) or unchanging (in which case it saves it in program memory as a constant value). By creating a volatile variable, the compiler knows not to discard the variable or to treat it as a constant. All datatypes can be declared as volatile variables.
The keyword static in front of a variable refers to how long the variable is active in memory. Variables without this keyword are automatic variables, which means that they come into existence when they are declared, and then expire whenever the function or control flow in which they reside has finished running.
A static variable exists in memory for as long as the program is running. This means that, even if they are declared with a certain value inside of a function or control flow block, they will not be re-declared subsequent times that function or control flow block executes. This allows for the use of non-global variables that can change within a function or control flow. All datatypes can be declared as static variables.
Example14.2.1.Automatic and static variables used in control flow.
The code below demonstrates iterative control flow (introduced in SectionΒ 14.9) where an automatic variable is used inside the control flow. On every iteration of this for loop, the variable a will be declared and set equal to β32. Unless the intent is for the final value of a to be β31 (which would negate the use of a loop in the first place), then the use of an automatic variable was incorrect.
for (unsigned char j = 0; j < 5; j++) {
char a = -32;
a++;
}
The code below demonstrates the use of iterative control flow with a static variable a. The variable a will only be declared when the loop is entered into for the first time. Changes to a will persist throughout multiple iterations of the loop. This means the final value of a in this loop will be β27.
The keyword const is used to denote a variable whose value will not change. The keyword can be used with all datatypes. This keyword is convenient to use with data arrays to define the number of elements to be stored into the array. Using a variable allows the number to be stored in a single location which can easily be modified in the future. If an array is defined using a variable to denote the number of elements, that variable must be a const variable. Most compilers will give a warning if attempting to assign a new value to a const variable inside of C code.