|
Answer» Storage Classes are used to specify a variable or function's characteristics. These characteristics include SCOPE, visibility, and lifetime, which allow us to track the presence of a variable during the course of a program's execution. The C programming language has four storage classes: - Auto: For all variables declared inside a function or a block, this is the default storage class. Auto variables can only be used within the block/function in which they were declared, not outside of it (which defines their scope). They can, however, be accessed outside of their scope using the idea of pointers, which are used to point to the actual memory address where the variables are stored. Whenever they are declared, they are given a garbage value by default.
- Extern: Extern storage class simply indicates that the variable is defined outside of the block in which it is used. Essentially, the value is ASSIGNED to it in a different block, and this can also be overwritten/changed in another block. A regular global variable can also be made extern by including the 'extern' keyword before its declaration or DEFINITION in any function or block. This means we're not creating a NEW variable; instead, we're merely using/accessing the global variable. Variables and functions can have their visibility extended using the extern keyword. The usage of extern in function declarations or definitions is unnecessary because functions are visible throughout the program by default. Its application is implicit. When you use extern with a variable, you're merely declaring it, not defining it.
- Static: This storage class is used to declare static variables, which are commonly used in C language programs. Static variables have the ability to retain their value even after they have been removed from their scope!. As a result, we can claim that they are only initialised once and exist till the program ends. Because they are not re-declared, no new memory is allocated. They have a limited scope that is limited to the role to which they were assigned. The program's global static variables can be accessed from anywhere in the code. The compiler assigns the value 0 to them by default.
- Register: This storage class declares register variables, which are similar to auto variables in functionality. The only difference is that if a free registration is available, the compiler tries to store these variables in the register of the CPU. If no free registers are available, these are stored solely in memory. The register keyword is used to define a few variables that will be accessed frequently in a program, which reduces the program's run time. The address of a register variable cannot be obtained via pointers, which is a CRUCIAL point to remember.
|