Month End Sale: Get Extra 10% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now

Storage Classes in C

Level : Intermediate
Mentor: Shailendra Chauhan
Duration : 00:02:00

What is a storage class in C?

A storage class specifier in the C programming language determines how variables are allocated, accessed, and stored in memory. It specifies a variable's range, lifetime, & visibility within a program.

Types of Storage Class in C

C has four different types of storage classes.

  1. Automatic Storage Class in C
  2. Static Storage Class in C
  3. Register Storage Class in C
  4. External Storage Class in C

Automatic Storage Class in C

Variables declared with this class are created when a function is called and removed when the function terminates. They are stored in stack memory.

Static Storage Class in C

Variables declared using this class keep their value between function calls. They are only initialized once and keep their value throughout the program's execution, which is kept in the data segment of memory.

Register Storage Class in C

This class is used to declare local variables that should be stored in a register rather than RAM, if possible, to improve access speed. However, current compilers are quite efficient in register allocation, therefore this keyword is often eliminated.

External Storage Class in C

Variables declared with this class are available across the program and can be referenced in various files. They are declared outside of any function with the extern keyword and are normally defined in a separate source file.

Advantages of Storage Class in C

  • Memory Management: Storage classes allow fine-grained control over memory allocation. You can optimize for speed (register) or code organization (static) depending on your requirements.
  • Modular Programming: The extern storage class allows variables to be shared across several source files, boosting code reuse and modularity.
  • Improved Efficiency: Storing register variables in CPU registers allows for faster access, which can considerably enhance performance.
  • Data Persistence: Static variables preserve their values across function calls, allowing you to keep the state within a function.

Disadvantages of Storage Class in C

  • Limited Control: Because register space may be limited, the compiler must ultimately determine whether to honor a register request.
  • Memory Overhead: Static variables persist throughout the program's life cycle, possibly increasing memory usage if not properly handled.
  • Global Variable Issues: Relying too heavily on global variables declared with extern might result in tight coupling and make testing and maintaining code more difficult.
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this