Storage Classes CHAPTER 7 Storage class There are












- Slides: 12


Storage Classes CHAPTER 7

Storage class There are two ways to characterize a variable : § § Data Type Storage Class 1. 2. 3. 4. Character Integer Float Point Double 1. 2. 3. 4. Automatic Static External Register

Storage class The storage class refers to : 1. Scope of a variable, the domain that a variable is identified 2. Permanence of a variable memory 3. Initial value if not specified

Scope of a Variable Scope of a variable : the domain that a variable is identified Local : When a variable is identified only in a function that is declared. Global : A variable that is identified in several functions In case of the same name for local and global variables, the local variable is preferred.

Permanence of a variable memory The memory assigned for a variable can be temporary or permanent. • • Temporary memory : As long as the function is executing, the variable retain its memory and is identified in the program. But when the execution goes out of that function then this variable is not defined any more and can not retain its memory. Permanent memory : The memory assigned for a variable is permanent as long as the program is running. It does not relate to where the execution line is. So this type of variables retain their memories.

Initial value When the initial value for a variable is not specified in declaration line, there are two options : q The initial value is zero q The initial value is not specified and is unpredictable

How to specify the storage class for a variable? Storag e Class Data Type Name of Variabl e Initial Value static int a, b, c=8; extern int n=-1; auto float f, k=. 5; register int u; If the storage class is not declared, the default storage class is automatic

storage class key word memory scope automatic auto temporary local extern permanent global zero static permanent local zero register temporary local register initial value unpredictable

Register storage class 1) Register variables are saved in CPU 2) It is similar to Automatic storage class 3) Speed of calculations with this type of variables is much higher than the other types 4) Usually only integer variables are considered 5) Number of register variables is limited (a few

External storage class To declare external storage class for a variable: Define the external variable - Outside of functions - No need to “ EXTERN” word - Giving initial value is permitted q Declare the external variable within the needed functions - Inside of functions - Add the “ EXTERN” word in declaration line - Giving initial value is not permitted q

Defference between declaration and definition of an external variable q q q External variable is defined outside but declared inside of a function EXTERN word used only in declaration Giving initial value is permitted only in definition