Storage Classes in C

Last Updated :

Every variable in C programming has two properties: type and storage class. Type refers to the data type of a variable. And, storage class determines the scope, visibility and lifetime of a variable.

Storage Classes are used to describe the features of a variable/function. These features basically include the scope, visibility and life-time which help us to trace the existence of a particular variable during the runtime of a program.

  1. scope –
    Where the value of the variable would be available inside a program.
  2. default initial value –
    Its default initial value.
  3. lifetime of that variable –
    For how long will that variable exist.

Types of Storage Classes :

There are 4 types of storage class: automatic, external, static, and register. These are explained as following below.

  1. Automatic storage class :
    This is the default storage class for all the variables declared inside a function or a block. Hence, the keyword auto is rarely used while writing programs in C language. The auto storage class is the default storage class for all local variables. It uses “auto” keyword to represent in Automatic storage class.

    {
       int year;
       auto int year;
    } 
  2. External storage class :
    External storage class simply tells us that the variable is defined elsewhere and not within the same block where it is used. It uses “extern” keyword to represent in Automatic storage class.

    {
       int year;
       extern int year;
    } 
  3. Static storage class :
    Static variable retains its value between multiple function calls or recursion calls. It uses “static” keyword to represent in Automatic storage class.

    {
       int year;
       static int year;
    } 
  4. Register storage class :
    Register storage class declares register variables which have the same functionality as that of the auto variables, but it stores in register of microprocessor. Program runs faster using this storage class. It uses “register” keyword to represent in Automatic storage class.

    {
       int year;
       register int year;
    } 

There is brief comparison between these storage classes :

Property Automatic External Static Register
Declaration Inside a function/block Outside all functions Inside or outside function Inside a function/block
Storage Stack Memory Memory Memory
Default Value Garbage 0 0 Garbage
Scope Local Global Local Local
Life End of block Till end of program Till end of program End of program



Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.

Comment
Next Article
CPP_Programmer Published 18 Aug, 2020 · 2 min read
Article Tags :

Similar Reads

  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read