Page 27 - Programming Microcontrollers in C
P. 27

12     Chapter 1  Introduction to C

                          be able to discard accesses to that value because the program never
                          alters the value. In such a circumstance, if you had an analog-to-digital
                          converter peripheral in your system, the program would never be re­
                          quired to read its return value more than once. “The program did not
                          change the value stored in the input location subsequent to the first
                          read, therefore its value has not changed and it is not necessary to read
                          the location again.” This will always produce wrong results. The key
                          word volatile indicates to the program that a variable must not be
                          optimized. Therefore, if the input location is identified as a vola­
                          tile variable, it will not be optimized and the problem will go away.
                          As a point of interest, accessing a volatile object, modifying an
                          object, modifying a file, or calling a function that does any of those
                          operations are all defined as side effects by the standard.


            Storage Classes, Linkage, and Scope

                              Additional modifiers are called storage classes and designate
                          where a variable  is to be stored and how it is initialized. These stor­
                          age classes are auto (for automatic), static, and malloced.
                          The first two storage classes are described in the following sections.
                          The storage class malloc provides dynamic memory allocation and
                          is discussed in detail in Chapter 2.

            Automatic variables
                              For local variables defined within a function, the default storage
                          class is auto. An automatic variable has the scope of the block in
                          which it is defined, and it is uninitialized when it is created. Auto­
                          matic variables are usually stored on the program stack, so space for
                          the variable is created when the function is entered. When the stack
                          is cleaned up prior to the return at the end of the function, all vari­
                          ables stored on the stack are deleted.
                              As we saw in our first program example, variables can be initial­
                          ized at the time of declaration by assigning the variable an initial value:
                   int rupt=17;

                              An automatic variable will be assigned its initial value each time
                          the block in which it is declared is entered. If the variable is not
                          initialized at declaration, it will contain the contents of uninitialized
                          memory, which can be any value.
   22   23   24   25   26   27   28   29   30   31   32