a.Scope (Visibility):
The scope of a variable indicates where the variable is accessible in the program and where it is not. The scope of a variable is determined only at compile time without creating any function call stack that occurs at runtime. In simple words, if you access a variable outside its scope, the compiler will throw an error.
b.Lifetime:
How long a variable stays in the appropriate location is defined in system memory or lifetime,which is the time between when memory is allocated to hold the variable and when it is freed.
Once a variable goes out of scope, its lifetime expires.
Life time of a variable is known as extent or range of a variable.
c.Memory Allocation:
The concept of memory allocation in C allows the C programmer to allocate memory at runtime. where functions or variables are stored in memory (CPU or RAM registers).
d.Initial Value:
The initial value is the default value assigned to the variable when it is declared.
Types of Storage Classes:
Storage classes or Memory classes in C language are declared in a program or block with memory class specifiers: auto, register, extern, static. There is another storage class specifier, "typedef", used syntactically, which does not reserve memory. Specifiers instruct the compiler to store variables.
1. Automatic (auto):
This is the default storage class that holds the values or objects for all the variables that are displayed in the function or block.
If a storage class is not specified, every variable defined in a function or block will default to the automatic storage class.
Therefore, the auto keyword is rarely used when writing C programs. Functions or block variables contained in the auto storage class are declared using the auto specifier. Variables in C are local to the block they are defined in and are discarded outside the block.
Clearly specifying the auto keyword for variables within a block or function forces their automatic stacking behaviour. This makes it more clear in code but is not used because of the default behaviour of local variables in C.
A Simple Program in Showing Automatic Storage Classes
#include <stdio.h>
int main()
{
auto int v=11;
{
auto int v=22;
{
auto int v = 33;
printf ( “%d “, v);
}
printf ( “%d “, v);
}
printf( “%d”, v);
}
The output of the Program:
33 22 11
Explanation:
Let understand the above program.
In the given program the variable v is declared three times.
The variable v with same name is defined three times in different blocks.
The program will compile and run successfully without any error.
The printf() function in innermost block will print 33 and variable 'v' inside this block will destroyed after the block ends.
This is followed by another block which prints 22 which is followed by an block which prints 11.
Automatic variables are initialized correctly; Otherwise you will get undefined values because the compiler does not assign an initial value to them.
2. Register (register):
“The register storage class is a required specifier that tells the compiler to store the object in a machine register.”
Variables defined as registers are allocated among CPU registers according to the size of memory remaining in the CPU. When you want to store local variables in a function or block in CPU registers instead of RAM to get quick access to these variables, you can use the register storage class. Register variables cannot be dereferenced i.e. & operator cannot be used for the register variable. The access time for register variable is faster.
For example, "counters" are good candidates for storing in registers.
register int counter;
Features of register variables:
i. Variables that we define as registers are memory allocations in CPU registers. This entirely depends on the size of memory left in the CPU.
ii. Its access time is much faster compared to automatic variables.
iii. A pointer can just as easily be stored in a register variable. This means that any register is capable of storing the address of a given variable.
iv. We cannot store a variable in a register because we cannot use more than one storage specifier for the same variable.
v. We cannot store a variable in a register because more than one storage specifier can not be used for the same variable.
vi. We use the register keyword for variables stored in CPU registers. However, whether a variable should be stored in a register is always up to the compiler.
vii. a register variable cannot have dereferenced. In other words, one cannot use '&' operator in case of register variables.
Scope: scope of register variable is within the block in which it is declared.
Lifetime: Same as automatic variables.
Memory Allocation: CPU register (if possible), otherwise stack (RAM).
Initial Value: the initial value of register variable is undefined.
C program demonstrating use of register Keyword.
#include <stdio.h>
int main()
{
register int a; // Declaring 'a' as a register variable
int b;
a = 100;
b = 200;
printf("Value of a: %d\n", a);
printf("Value of b: %d\n", b);
return 0;
}
Output:
Value of a: 100
Value of b: 200
In the above example, we have defined 'a' as a register variable. On the other hand, whether it is actually stored in a CPU register depends on the behavior of the compiler. The main thing you need to consider is that the register will not match the variables whose addresses are selected using the & operator, because it is not easy to directly deal with the register. Therefore, trying to apply registry changes results in a compilation error.
Static storage class
Static storage class or the keyword is a type of storage class in C and C++ language that defines a variable with a fixed lifetime. This means the variable is allocated in memory when the program starts and is deleted only after the program terminates. This means that the variable retains its value between function calls.
Keyword used: Static
Lifetime: Throughout the entire program
Default value Zero
C program for static storage class.
#include <stdio.h >
int statfunc()
{
static int tot =40;
tot++;
return tot;
}
int main()
{
printf("total = %d \n", statfunc());
printf("total= %d \n", statfunc());
return(0);
}
Output
total = 41
total = 42
Explanation:
The statfunc () function in above program has a variable called tot, initially set to 40. The static keyword is used to create a static variable, meaning that the variable’s value will be conserved or preserved between function calls.
The statfunc function increments the value of the sum by 1 and then returns the new value. When the statfunc() function is called, it will always return the next value in the sequence (41, 42)
In the main function, statfunc() is called twice, and the return value is printed using the printf function.
The first time statfunc() is called, it returns the initial value of the sum, which is 41. The second time it’s called, it increments the sum to 42 and returns that value.
So, here also, the Function statfunc() returns the value of sum by increasing it every time, and the main function is just calling the function and printing the returned value.
4.External (extern):
Extern means an external storage class. External storage class is used when we have functions or global variables shared between two or more files.
The word extern is used to define a global variable or function in another file to refer to a variable or function that is already defined in the original file.
Variables defined with the extern keyword are called global variables. These variable are available to access during the entire program. Note that an extern variable cannot be initialized because it is already defined in the original base file.
Example,
extern void display();
File1: main.c
#include <stdio.h>
extern xt;
main()
{
printf("the value of the external integer xt is = %d\n", xt);
return 0;
}
File2: original.c
#include <stdio.h>
x=48;
Result:
the value of the external integer xt is = 48
Previous Topic:-->> realloc() in C || Next topic:-->>Graphics in C.