Q22: Explain storage class in detail.

 

A22: Storage classes define the scope, lifetime, and visibility of variables within a program. Each variable in C has a storage class, which determines how the variable is stored in memory and how it can be accessed.

 

There are four storage classes in C:

 

1.      Automatic Storage Class (auto):

Memory allocated and deallocated automatically to the variable.

They are used for local variables.

Variable contains garbage value unless initialized.

 

2.      Register Storage Class (register):

It is same as auto but variable stored in CPU registers.

‘register’ keyword is a hint to compiler to optimize access.

The variable does not have any memory location.

 

3.      Static Storage Class (static):

Variable has fixed memory location throughout the program.

They are initialized only once, and their values persist.

They are accessible within the entire source file.

They are initialized to zero by default.

 

4.      External Storage Class (extern):

The variables which are declared outside all function are known as external variables.

They can be used by all the function.

The ‘extern’ keyword is used to declare variable.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.