Storage Classes in C

 

Storage Classes in C

Storage classes in C are used to determine the lifetime, visibility, memory location, and initial value of a variable. There are four types of storage classes in C. C provides several storage classes, each of which serves a specific purpose. The five primary storage classes in C are:

Storage Classes in C


1. Automatic

2. External

3. Static

4. Register

Storage ClassesStorage PlaceDefault ValueScopeLifetime
autoRAMGarbage ValueLocalWithin function
externRAMZeroGlobalTill the end of the main program Maybe declared anywhere in the program
staticRAMZeroLocalTill the end of the main program, Retains value between multiple functions call
registerRegisterGarbage ValueLocalWithin the function

1. Automatic: Variables declared with the auto storage class have local scope and are created and destroyed every time the block in which they are defined is entered and exited. In modern C programming, the auto keyword is rarely used explicitly because, by default, local variables have automatic storage class.

c
#include <stdio.h> int main() { auto int x = 10; // 'auto' is optional, as local variables are automatic by default printf("x = %d\n", x); return 0; }

Output


x = 10
In this example, the x variable is declared with the auto storage class, but this keyword is optional as local variables are automatically treated as "auto" by default. The variable x has a local scope and is automatically destroyed when the block is exited.


2. External (extern):


External variables are declared outside of any function and have global visibility.

They can be accessed from other source files by using the 'extern' keyword
c
#include <stdio.h>
int globalVariable = 42; extern int globalVariable; int main() { printf("Global Variable: %d\n", globalVariable); return 0; }

Output

c
Global Variable: 42
 
3. Static:  Variables declared with the static storage class have a longer lifetime than automatic variables. They retain their values between function calls and have file scope, meaning they are visible within the entire file in which they are defined
c
#include <stdio.h> void increment() { static int counter = 0; counter++; printf("Counter: %d\n", counter); } int main() { increment(); // Counter: 1 increment(); // Counter: 2 return 0; }

Output

Counter: 1 Counter: 2
In this example, the counter variable is declared with the static storage class. It retains its value between function calls and has file scope. As a result, the variable counter is not re-initialized to 0 every time the increment function is called.

4. Register: The register storage class is used to suggest to the compiler that a variable should be stored in a CPU register for faster access. However, modern compilers are capable of optimizing variable storage automatically, so this keyword is rarely used

c
#include <stdio.h> int main() { register int i; // Suggesting the compiler to store 'i' in a register int sum = 0; for (i = 1; i <= 10; i++) { sum += i; } printf("Sum: %d\n", sum); return 0; }

Output
c
Sum: 55

Post a Comment

0 Comments