|
Answer» Storage Classes are used to define the properties of a variable or function. SCOPE, visibility, and longevity are all qualities that allow us to track the presence of a variable during the execution of a programme. There are four storage classes in the C programming language: - Auto: This is the default storage class for all variables specified inside a function or a block. Auto variables can only be utilized within the block/function in which they were defined; they can't be used outside (which defines their scope). They can, however, be accessed outside of their scope by using pointers, which point to the memory address where the variables are kept. They are given a garbage value by default whenever they are declared.
- Static: Static variables are often used in C language programs, and this storage type is used to declare them. Even after they have been removed from their scope, static variables can keep their value! As a result, we MAY assert that they are only initialized once and live only till the programme terminates. No fresh memory is allocated because they are not declared again. They have a limited scope that is restricted to the role they have been allocated. Global static variables in the programme can be accessed from anywhere in the code. By default, the compiler assigns a value of 0 to these.
- Register: This storage class declares register variables, which are functionally comparable to auto variables. The only difference is that if there is a free registration available, the compiler will attempt to store these variables in the CPU's register. If no free REGISTERS are available, the data is kept in memory alone. The register keyword is used to create a few variables that will be accessed frequently in a programme, hence speeding up its execution. A register variable's address cannot be accessed via pointers, which is an important fact to note.
- Extern: The storage class extern merely means that the variable is defined outside of the block in which it is referenced. In essence, the value is ASSIGNED to it in another block, which can then be overwritten/changed in another block. Any function or block can make a regular global variable extern by using the 'extern' keyword before its declaration or definition. This INDICATES that instead of establishing a new variable, we're simply accessing and using the global variable. The extern keyword can be used to extend the visibility of variables and functions. Because functions are visible throughout the programme by default, the use of extern in function declarations or definitions is unnecessary. Its use is self-evident. When you use extern to declare a variable, you are not actually defining it.
|