Wednesday, March 18, 2009

What Are Static Variables?

An additional variable type must be mentioned at this point, the "static" variable. By putting the reserved word "static" in front of a variable declaration within a function, the variable or variables in the declaration are static variables and will stay in existence from call to call of the particular function.

By putting the same reserved word in front of an external variable, one outside of any function, it makes the variable private and not accessible to use in any other file. This implies that it is possible to refer to external variables in other separately compiled files, and that is true. Examples of this usage will be given in chapter 14 of this tutorial.

Using The Same Name Again


Refer to the function named "head2". It contains another definition of the variable named "count". Even though "count" has already been defined as a global variable, it is perfectly all right to reuse the name in this function. It is a completely new variable that has nothing to do with the global variable of the same name, and causes the global variable to be unavailable in this function. This allows you to write programs using existing functions without worrying about the variables that interface with the functions.


What Is A Register Variable?


Now to fulfill a promise made earlier about what a register variable is. A computer can keep data in a register or in a memory. A register is much faster in operation than memory but there are very few registers available for the programmer to use. If there are certain variables that are used extensively in a program, you can designate that those variables are to be stored in a register if possible in order to speed up the execution of the program. Depending on the computer and the compiler, a small number of register variables may be allowed and are a desired feature. Check your compiler documentation for the availability of this feature and the number of register variables. Most compilers that do not have any register variables available, will simply ignore the word "register" and run normally, keeping all variables in memory.

Register variables are only available for use with integer and character type variables. This may or may not include some of the other integer-like variables such as unsigned, long, or short.

Check the documentation for your compiler.

Register variables are allowed in Hi Tech C, with up to four non-pointer register variables (in 68000 registers D4 to D7), and up to three pointer register variables (in A3 to A5). This usage does not conflict with the 1616/OS usage of D0 to D2 and A0 to A2). As MS-DOS compilers typically only allow two register variables, many programmers do not make extensive use of register variables, so you can often gain a little extra speed when converting programs by a wider use of register variables (the compiler will ignore the register variable request if no registers are available, and treat the variable as an ordinary one).

Google Search

Custom Search