Storage Class
Types of Storage Classes
There are four storage classes namely,
What does the storage class determine?
From C compiler’s point of view, a variable-name identifies some physical location within the computer where the string of bits representing the variable’s value is stored. There are basically two kinds of locations in a computer where such a value may be kept – Memory and CPU registers. It is the variable’s storage class that determines in which of these two types of locations, the value is stored.
A variable’s storage class tell
Which storage class to use when?
- In C, all variables have data types and storage classes.
- To fully define a variable, one needs to mention not only its ‘type’ but also its ‘storage class’. In other words, not only do all variables have a data type, they also have a ‘storage class’.
- Data type refers to the type of information represented by a variable (i.e. integer, character, float and double etc.).
- Storage classes determine the lifetime of the storage, associated with the variable.
- Storage classes also refer to the scope of the variables within the program.
- Scope of a variable is defined as the region over which the variable is visible or valid. Scope is always established by a set of flower braces.
- Storage classes have defaults.
- If don’t specify the storage class of a variable depending on the context in which the variable is used. Thus, variables have certain default storage classes.
Types of Storage Classes
There are four storage classes namely,
- Automatic storage class
- Register storage class
- Static storage class
- External storage class
What does the storage class determine?
From C compiler’s point of view, a variable-name identifies some physical location within the computer where the string of bits representing the variable’s value is stored. There are basically two kinds of locations in a computer where such a value may be kept – Memory and CPU registers. It is the variable’s storage class that determines in which of these two types of locations, the value is stored.
A variable’s storage class tell
- Where the variable would be stored
- What will be the initial value of the variable, if initial value is not specifically assigned (i.e. the default initial value).
- What is the scope of the variable, i.e. in which functions the value of the variable would be available.
- What is the life of the variable; i.e. how long would the variable exist.
Which storage class to use when?
- Use static storage class only if want the value of a variable to persist between different function calls.
- Use register storage class for only those variables that are being used very often in a program. Reason is, there are very few CPU registers at our disposal and many of them might be busy doing something else. Make careful utilization of the scarce resources. A application of register storage class is loop counters, which get used number of times in a program.
- Use extern storage class for only those variables that are being used by almost all the functions in the program. This would avoid unnecessary passing of these variables as arguments when making a function call. Declaring all the variables as extern would amount to a lot of wastage of memory space because these variables would remain active throughout the life of the program.
- If you don’t have any of the express needs mentioned above then use the auto storage class.