Global variables in C Programming Language

Rumman Ansari   Software Engineer   2019-03-31   9364 Share
☰ Table of Contents

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 :

  1. Auto Storage Class
  2. Static Storage Class
  3. Extern Storage Class
  4. Register Storage Class

num = 1
num = 2
num = 3
num = 4
num = 5

Storage class of variable Determines following things –

  1. Where the variable is stored
  2. Scope of Variable
  3. Default initial value
  4. 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.

Automatic ( Auto ) storage class in C Programming

  1. This is default storage class
  2. All variables declared are of type Auto by default 
  3. In order to Explicit declaration of variable use ‘auto’ keyword
auto int num1 ;   // Explicit Declaration  
Features :
 Storage Memory
 Scope Local / Block Scope
 Life time
Exists as long as Control remains in the block
 Default initial Value
Garbage

Live Example :

void main()
{
auto mum = 20 ;
{
auto num = 60 ;
printf("nNum : %d",num);
}
printf("nNum : %d",num);
}

Output :
Num : 60
Num : 20

Two variables are declared in different blocks , so they are treated as different variables
External ( extern ) storage class in C Programming

  1. Variables of this storage class are “Global variables”
  2. Global Variables are declared outside the function and are accessible to all functions in the program
  3. Generally , External variables are declared again in the function using keyword extern
  4. In order to Explicit declaration of variable use ‘extern’ keyword
extern int num1 ;   // Explicit Declaration  
Features :
 Storage Memory
 Scope Global / File Scope
 Life time
  • Exists as long as variable is running
  • Retains value within the function
 Default initial Value
Zero

Live Example :
int num =  75 ;  

void display();

void main()
{
extern int num ;
printf("nNum : %d",num);
display();
}

void display()
{
extern int num ;
printf("nNum : %d",num);
}

Output :
Num : 75
Num : 75

Note :

  1. Declaration within the function indicates that the function uses external variable
  2. Functions belonging to same source code , does not require declaration (no need to write extern)
  3. 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 :

  1. register keyword is used to define local variable.
  2. Local variable are stored in register instead of RAM.
  3. As variable is stored in register, the Maximum size of variable = Maximum Size of Register
  4. unary operator [&] is not associated with it because Value is not stored in RAM instead it is stored in Register.
  5. This is generally used for faster access.
  6. 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 ?

  1. Whenever we declare any variable inside C Program then memory will be randomly allocated at particular memory location.
  2. 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 (&).
  3. If we store same variable in the register memory then we can access that memory location directly without using the Address operator.
  4. 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.