0
5.2kviews
Explain different storage classes.
1 Answer
0
166views

The storage class of a variable is a set of properties about the variable.

The storage class of a variable determines 3 things: –

  1. Scope: the sections of code that can use the variable. (i.e., variable visibility domain)

  2. Linkage: how a variable can be used in a multiple source-file program.

  3. Storage duration: how long the variable persists in memory.

There are 4 types of storage classes:

  1. Auto 2. Register 3. Static 4. Extern

Auto Storage Class:

A variable declared inside a function without any storage class specification, is by default an automatic variable. They are created when a function is called and are destroyed automatically when the function exits. Automatic variables can also be called local variables because they are local to a function. By default they are assigned garbage value by the compiler.

Register Storage Class:

Register variable inform the compiler to store the variable in register instead of memory. Register variable has faster access than normal variable. Frequently used variables are kept in register. Only few variables can be placed inside register.

Static Storage Class:

The static storage class instructs the compiler to keep a local variable in existence during the lifetime of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls.

static is initialized only once and remains into existence till the end of program.

Extern Storage Class:

The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern', the variable cannot be initialized as all it does is point the variable name at a storage location that has been previously defined.

When you have multiple files and you define a global variable or function, which will be used in other files also, then extern will be used in another file to give reference of defined variable or function. Just for understanding, extern is used to declare a global variable or function in another file.

Please log in to add an answer.