Page 29 - Programming Microcontrollers in C
P. 29

14     Chapter 1  Introduction to C

                              Inside of a function, the following declaration is made:

                   static int keep = 1;
                              When the program is loaded and executed, the value 1 is assigned
                          to keep. Thereafter, each time the function is entered, keep will
                          not be initialized but will retain the value assigned to it the last time
                          the function was executed.
                              Global variables can be designated as static. A global vari­
                          able that is static is similar to a conventional global variable with
                          the exception that it can be accessed only from the file in which it is
                          declared.
                              If there is an external variable that is declared in one file that is to
                          be accessed by a function defined in another file, the function must
                          notify the compiler that the variable is external with the use of the
                          keyword extern. The following is an example of such an access.
                              In file 1:

                   int able;


                   int main(void)
                   {
                       long quickstart(void);
                       long r;
                       .
                       .
                       .
                       able=17;
                       l=quickstart();
                       .
                       .
                   }

                          In file 2:

                   long quickstart(void)
                   {
                       extern int able;
                       .
                       /* do something with able */
                       .
   24   25   26   27   28   29   30   31   32   33   34