|
Answer» Storage Classes are used to define a variable's or function's properties. The scope, visibility, and durability of a variable are all characteristics that allow us to track its presence during the execution of a program. In the C programming language, there are four storage classes: - Auto: All variables supplied inside a function or a block are stored in this class by default. Auto variables can only be used within the block/function in which they were defined; they cannot be used outside of that block/function (which defines their scope). They can be accessed outside of their scope, though, by using pointers that point to the memory address where the variables are stored. Whenever they are declared, they are given a garbage value by default.
- Extern: The storage class is external. The term "extern" simply denotes that the variable is defined outside of the block in which it is used. In essence, it is given a value in another block, which can subsequently be overwritten or updated in another block. An ordinary global variable can be made extern by using the 'extern' keyword before its declaration or definition in any function or block. This means that rather than creating a NEW variable, we're just accessing and using the global variable. The extern keyword can be used to make variables and functions more visible. The usage of extern in function declarations or DEFINITIONS is unnecessary because functions are visible throughout the programme by default. Its application is self-evident. When declaring a variable with extern, we are not really going to define it.
- Register: This storage class declares register variables, which are similar to auto variables in terms of functionality. The main difference is that the compiler will attempt to store these variables in the CPU's register if a free registration is available. If there are no free registers, the data is stored solely in memory. The register keyword is used to define a few variables that will be accessed frequently throughout a programme, allowing it to run faster. The address of a register variable cannot be accessed via pointers, which is a crucial detail to remember.
- Static: This storage type is used to define static variables, which are commonly UTILISED in C language programs. Static variables can preserve their value even after they've been removed from their scope! As a result, we can say that they are only initialised once and only exist till the program ends. Because they are not declared again, no new RAM is allocated. They have a limited scope, which is confined to the role that has been assigned to them. The program's global static variables can be accessed from ANYWHERE in the CODE. The compiler sets a value of 0 to these by default.
|