Page 28 - Programming Microcontrollers in C
P. 28

Storage Classes, Linkage, and Scope       13

                              Another class of variable is register. A register class variable
                          is automatic, i.e., it comes into being at the beginning of the block in
                          which it is defined and it goes out of scope at the end of the block. If
                          a register is available in the computer, a register variable
                          will be stored in a register. To define a register variable, you should
                          use the form

                   register int roger=10;
                          These variables can be long, short, int, or char.
                              When a register is not available, a register variable will be
                          stored just like any other automatic variable. A programmer might
                          consider the use of register variables in code that contains “tight
                          loops” to save the time of memory accesses while executing the loop.
                          A bit of advice. Compilers have improved continuously over the past
                          years. With today’s compilers, the optimizers are so efficient that the
                          compiler can probably do a better job of assigning register vari­
                          ables than the programmer. Therefore, it makes little sense to specify a
                          lot of register variables just to improve the efficiency of your code.

            Static variables

                              Sometimes you might want to assign a value to a variable and
                          have it retain that value for later function calls. Such a variable can
                          be created by calling it static at its definition. There are two groups
                          of static variables: Local static variables which have a scope
                          of the function in which they are defined, and global or external
                          static class variables. Unless otherwise declared, all static class
                          variables are initialized to 0 when they are created.
                              There are two groups of external static variables. Any exter­
                          nal variable is a static class variable. It is automatically initialized
                          to the value 0 when the program is loaded unless the value is other­
                          wise declared in the definition statement. An external variable that is
                          declared as static in its definition statement like

                   static int redoubt;
                          will have file scope. Remember normal external variables can be
                          accessed from any module in the entire program.  A static exter­
                          nal variable can be accessed only from within the file in which it is
                          defined. Note that static variables are not stored on the stack, but
                          rather stored in a static data memory area.
   23   24   25   26   27   28   29   30   31   32   33