0 votes
104 views
in C programming by (98.9k points)
edited
Explain different storage classes.

1 Answer

0 votes
by (98.9k points)
selected by
 
Best answer

The different locations in the computer where we can store data and their accessibility, initial values etc. vary based on the way they are declared. These different ways are termed as different storage classes. In C, we have four storage classes, namel

  1. Automatic
  2. Register
  3. Static
  4. External or Global
    Let us see these storage classes one by one
  1. Automatic storage class
    In this case data is stored in memory
    The initial value of such a variable is
    garbage.
    The scope of the variable is local i.e. limited
    to the function in which it is defined.
    The life of such variables is till the control
    remains in the particular function where it is
    defined.
    For e.g.:
    int i; or auto int i;
    In all our programs till now we have been
    using the automatic storage class for our
    variables.
  1. Register storage class
    In this case data is stored in CPU register
    The initial value of such a variable is
    garbage.
    The scope of the variable is local i.e. limited
    to the function in which it is defined.
    The life of such variables is till the control
    remains in the particular function where it is
    defined.
    For e.g.:
    register int i;
    In this case the data is stored in a small
    memory inside the processor called as its
    registers.
    The advantage of such storage class is that
    since the data is in the processor itself, its
    access and operations on such data is faster.
    There is a limitation on the size of the data
    that can be declared to be register storage
    class. The data should be such that it doesn’t
    require more than 4 bytes. Hence double and
    long double data types cannot be declared as
    register.
    Also there is a limitation on the maximum
    number of variables in a function that can be
    of register class. The limitation is that a
    maximum of 3 register class variable can be
    declared in a function.
  1. Static storage class
    In this case data is stored in memory
    The initial value of such a variable is zero.
    The scope of the variable is local i.e. limited
    to the function in which it is defined.
    The life of such variables is till the program
    is alive.
    For e.g. :
    static int i;
    If a variable is declared static, its value
    remains unchanged even if the function
    execution is completed.

Related questions

0 votes
1 answer 172 views
asked May 27, 2022 in C programming by Doubtly (98.9k points)
0 votes
1 answer 129 views
0 votes
0 answers 68 views
asked May 28, 2022 in C programming by Doubtly (98.9k points)

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

537 users

...