Storage classes in C determine a variable’s scope, visibility, lifetime, and storage location. They include auto, extern, static, and register. The default initial value varies based on the storage class. Scope defines variable visibility, while lifetime dictates its persistence in memory.
Read more: C – Storage Classes- Scope: The extent to which different parts of a program have access to the variable, determining its visibility.
- Lifetime refers to the duration for which the variable persists in memory, encompassing the allocation and deallocation of its storage. The scope also influences a variable’s lifetime.
- variable types(Global variable)
Variable Type | Lifetime | Scope |
---|---|---|
Global Variable | Only destroyed when the program terminates | Only the program |
Local Variable | Allocated memory when entering the function, destroyed when leaving | Limited to the function |
- syntax: storage_class_specifier data_type variable_name;
Specifiers | Lifetime | Scope | Default Initialize |
---|---|---|---|
auto | Block (inside function) | Block | Uninitialized |
register | Block (stack or CPU register) | Block | Uninitialized |
static | Program | Block or compilation unit | Zero |
extern | Program | Block or compilation unit | Zero |
(none) | Dynamic (heap) | nil | Uninitialized |
Contents
Auto Keyword
A variable declared inside a function without any specified storage class is termed an auto variable.
- Only usable within functions.
- Created and destroyed automatically upon function call and exit, respectively.
- Compiler assigns them garbage values by default.
- The stack memory stores local variables.
- By default, every local variable in a function is of auto storage class.
Example:
void f() { int i; // auto variable auto int j; // auto variable }
Global variable
- A variable declared outside any function is a global variable.
- Any function in the program can change its value.
- initializing
- int – 0
- char – \0
- float – 0
- double -0
- pointer – null
Register Keyword
The “register” keyword specifies that local variables are stored in a register for rapid access, particularly for counters. These variables offer quicker access than normal ones, but only a limited number can be placed in registers. While the compiler sees “register” as a suggestion, it ultimately decides. Typically, compilers optimize and determine variable allocation.
Register Note 1:
Attempting to access the address of a register variable results in a compile error. For instance:
int main() { register int i = 10; int *a = &i; // Compile error printf("%d", *a); getchar(); return 0; }
Register Note 2:
You can use the register keyword with pointer variables. Below is a valid example:
int main() { int i = 10; register int *a = &i; printf("%d", *a); // Output: 10 getchar(); return 0; }
Resistor Note 3:
Register is a storage class, and C prohibits multiple storage class specifiers for a variable. Therefore, you cannot use register with static.
int main() { int i = 10; register static int *a = &i; // Error printf("%d", *a); getchar(); return 0; }
Register Note 4:
There’s no limit to the number of register variables in a C program. However, the compiler may choose to allocate some variables to registers and others not.
static keyword
- static means its take only single or same memory.
- static is initialized only once and remains into existence till the end of program
- static assigned 0 (zero) as default value by the compiler.
- A static local variables retains its value between the function call and the default value is 0.
- If a global variable is static then its visibility is limited to the same source code.
- Static variables stored in Heap(Not in Stack memory)
- The following function will print 1 2 3 if called thrice.
- Example
- void f() { static int i; ++i; printf(“%d “,i); }
- In “static int a[20];”, variable “a” is declared as an integer type and static. When a variable is declared as static, it is automatically initialized to the value ‘0’ (zero).
What is a static function?
- A function prefixed with the static keyword is called a static function.
- You would make a function static if it should only be called within the same source code or be visible to other functions in the same file.
- static has different meanings in in different contexts.
- When specified on a function declaration, it makes the function local to the file.
- When specified with a variable inside a function, it allows the vairable to retain its value between calls to the function
storage class specifier
- typedef – typedef is the storage class specifier.
- It does not reserve the storage.
- It is used to alias the existing type. Also used to simplify the complex declaration of the type.
- reference links
volatile
- The compiler omits optimization for objects declared as volatile because code outside the current scope can change their values at any time.
Advantages and Disadvantages
Storage Class | Advantage | Disadvantage |
---|---|---|
auto | Automatically allocated and deallocated within functions | Garbage values if not explicitly initialized |
register | Faster access due to storage in CPU registers | Limited availability of register space; Compiler-dependent |
static | Persistent value across function calls | Global scope may lead to namespace pollution; May increase memory usage |
extern | Allows sharing of variables across multiple files | Global scope may lead to namespace pollution |
Interview Questions
What are storage classes in ‘C’ language?
- automatic class
- static class
How many storage class specifiers in “C” language?
- auto,
- register,
- static
- extern
How many variables scopes are there in “C” language?
- Body.
- Function.
- Program.
- File.