Understanding Global Variables in C: Definition and Usage
Table of Content:
Storage Class in C Programming :
- Every Variable in a program has memory associated with it
- Memory Requirement of Variables is different for different types of variables
- In C , Memory is allocated & released at different places
Storage Class : Summary
Term | Definition |
Scope | Region or Part of Program in which Variable is accessible |
Extent | Period of time during which memory is associated with variable |
Storage Class | Manner in which memory is allocated by the Compiler for VariableDifferent Storage Classes |
Different Storage Classes :
- Auto Storage Class
- Static Storage Class
- Extern Storage Class
- Register Storage Class
num = 1
num = 2
num = 3
num = 4
num = 5
Storage class of variable Determines following things –
- Where the variable is stored
- Scope of Variable
- Default initial value
- Lifetime of variable
A. Where the variable is stored :
Storage Class determines the location of variable , Where it is declared. Variables declared with auto storage classes are declared inside main memory whereas variables declared with keyword register are stored inside the CPU Register.
B. Scope of Variable
Scope of Variable tells compile about the visibility of Variable in the block. Variable may have Block Scope , Local Scope and External Scope. Wiki has defined variable scope as –
[box]In computer programming, a scope is the context within a computer program in which a variable name or other identifier is valid and can be used, or within which a declaration has effect[/box]
C. Default Initial Value of the Variable
Whenever we declare a Variable in C, garbage value is assigned to the variable. Garbage Value may be considered as initial value of the variable. C Programming have different storage classes which has different initial values such as Global Variable have Initial Value as 0 while the Local auto variable have default initial garbage value.
D. Lifetime of variable
Lifetime of the = Time Of - Time of Variable variable Declaration Destruction
Suppose we have declared variable inside the main function then variable will be destroyed only when the control comes out of the main .i.e end of the program.
- This is default storage class
- All variables declared are of type Auto by default
- In order to Explicit declaration of variable use ‘auto’ keyword
auto int num1 ; // Explicit Declaration
Storage | Memory |
Scope | Local / Block Scope |
Life time |
Exists as long as Control remains in the block |
Default initial Value |
Garbage |
void main()
{
auto mum = 20 ;
{
auto num = 60 ;
printf("nNum : %d",num);
}
printf("nNum : %d",num);
}
Num : 60
Num : 20
Two variables are declared in different blocks , so they are treated as different variables
- Variables of this storage class are “Global variables”
- Global Variables are declared outside the function and are accessible to all functions in the program
- Generally , External variables are declared again in the function using keyword extern
- In order to Explicit declaration of variable use ‘extern’ keyword
extern int num1 ; // Explicit Declaration
Storage | Memory |
Scope | Global / File Scope |
Life time |
|
Default initial Value |
Zero |
int num = 75 ;
void display();
void main()
{
extern int num ;
printf("nNum : %d",num);
display();
}
void display()
{
extern int num ;
printf("nNum : %d",num);
}
Num : 75
Num : 75
Note :
- Declaration within the function indicates that the function uses external variable
- Functions belonging to same source code , does not require declaration (no need to write extern)
- If variable is defined outside the source code , then declaration using extern keyword is required
We know the basic concept of storage class in c programming. In this tutorial we will be learning the register storage class.
Register Storage Class :
- register keyword is used to define local variable.
- Local variable are stored in register instead of RAM.
- As variable is stored in register, the Maximum size of variable = Maximum Size of Register
- unary operator [&] is not associated with it because Value is not stored in RAM instead it is stored in Register.
- This is generally used for faster access.
- Common use is “Counter“
Syntax
{ register int count; }
Register storage classes example
#include int main() { int num1,num2; register int sum; printf("\nEnter the Number 1 : "); scanf("%d",&num1); printf("\nEnter the Number 2 : "); scanf("%d",&num2); sum = num1 + num2; printf("\nSum of Numbers : %d",sum); return(0); }
Why we need Register Variable ?
- Whenever we declare any variable inside C Program then memory will be randomly allocated at particular memory location.
- We have to keep track of that memory location. We need to access value at that memory location using ampersand operator/Address Operator i.e (&).
- If we store same variable in the register memory then we can access that memory location directly without using the Address operator.
- Register variable will be accessed faster than the normal variable thus increasing the operation and program execution. Generally we use register variable as Counter.
Note : It is not applicable for arrays, structures or pointers.
Summary of register Storage class
Keyword | register |
---|---|
Storage Location | CPU Register |
Initial Value | Garbage |
Life | Local to the block in which variable is declared. |
Scope | Local to the block. |