Storage Classes in C
1
Scope and Lifetime of a Variable in C
Language
Scope determines the region in a C program where a
variable is available to use.
Visibility of a variable is related to the accessibility of
a variable in a particular scope of the program
Lifetime of a variable is for how much time a variable
remains in the system's memory.
2
Scope of a variable
• Scope is defined as the availability of a
variable inside a program, scope is basically
the region of code in which a variable is
available to use.
• There are four types of scope:
– file scope(Global Scope)
– block scope,
– function scope and
– prototype scope.
3
Global vs Local Scope
Global scope :
When variable is defined outside all functions.
It is then available to all the functions of the program and
all the blocks program contains.
Local scope :
When variable is defined inside a function or a block, then
it is locally accessible within the block and hence it is a
local variable.
4
Global vs Local Scope
int global = 100; // global variable declared
void main()
{
int local = 10; // local variable declared
printf("Global variable is %d",global);
printf("Local variable is %d",local);
func1();
}
void func1()
{
printf("Global inside func1 is %d",global);
5
FILE SCOPE(GLOBAL SCOPE)
• File scope of variables in C is defined as having
the availability of the variable throughout the
file/program.
• It means that the variable has a global scope,
and it is available all around for every function
and every block in the program.
6
#include <stdio.h>
// Calling a function that uses
the global variable
// Global variable with global scope
printGlobalVar();
int globalVar = 10;
// Modifying the global
// Function that uses the global
variable
variable
globalVar = 20;
void printGlobalVar() {
printf("After modifying in
printf("Inside the function: %d\n",
main function: %d\n",
globalVar); globalVar);
}
return 0;
int main() { }
// Accessing the global variable
within the main function
printf("Inside the main function:
%d\n", globalVar); Output:
Inside the main function: 10
Inside the function: 10
After modifying in main function: 207
8
BLOCK SCOPE
• Block scope of variables in C is defined as
when the variable has a limited scope, and the
memory occupied by the variable will be
deleted once the execution of the block ends.
• The variable is not accessible or available
outside the block.
• A block of code can be defined in curly braces
{code_block}.
9
10
Function Scope
• Function scope of variables in C begins with the
left curly brace { and ends with a closing right
curly brace }.
• A variable declared inside a function has a
function scope.
• It has been allocated memory when the function is
called, and once the function returns something,
the function execution ends and with it, the
variable goes out of scope, i.e. it gets deleted from
the memory.
11
12
Function Prototype Scope
• Function prototype scope of variables in C are
declared in some function as its parameters.
• These variables are similar to the function
scope variables where a variable's memory
gets deleted once the function execution
terminates.
13
14
Lifetime of a variable
• Lifetime of a variable is defined as for how
much time period a variable occupies a valid
space in the system's memory
• Once the variable is out of scope its lifetime
ends.
• Lifetime is also known as the range/extent of a
variable.
• Types of lifetime
– static lifetime
– automatic lifetime
– dynamic lifetime. 15
Static Lifetime
• The static variable is defined using the static
keyword.
• Its scope depends on the area of its declaration.
– If a static variable is defined within a function, it is a local
variable.
– If declared outside the function, its scope is global.
• A static variable statically allocated memory to the
variable and its lifetime throughout the program.
• It holds its value whenever a program is called.
• The default value of the static variable is 0.
• Example :
static int count = 0; 16
Static in C
17
18
Properties of static variable
• A static variable is destroyed only after the whole
program gets executed. It does not depend on the
scope of the function in which it is declared.
• A static variable has a property to retain its value
from its previous scope. This means that its value
does not get re-initialized if the function in which it
is declared gets called multiple times.
• If no value is assigned to a static variable, by
default, it takes the value 0.
• The memory of the static variable is available
throughout the program but its scope is only
restricted to the block where it is declared. 19
Automatic Lifetime
• All variables declared in C programming are
automatic by default.
• You can use the auto keyword to declare the
automatic variable.
• An automatic variable is a local variable whose
lifetime is within the code only.
• Its default value is garbage.
• Example :
{ // block of code
int auto_lifetime_var = 0;
} 20
21
22
Dynamic Lifetime
• Objects/Variables which are made during the run-time
of a C program using the Dynamic Memory Allocation
concept using the malloc(), calloc() functions in C are
stored in the memory until they are explicitly removed
from the memory using the free() function in C or
delete operator in C++ and these variables are said to
have a dynamic lifetime.
• These variables are stored in the heap section of our
system's memory, which is also known as the
dynamic memory.
• Example :
int *ptr = (int *)malloc(sizeof(int));
23
24
Explanation :
• Variables num2 and j as well as function
parameters a and b are local to the function
func1() and have function scope and prototype
scope respectively.
• Variables declared in function func2() and main()
function are not visible in func1().
• The variables num1 and num2 are global
variables having file scope.
• num1 is visible in func1() but num2 is
hidden/invisible by the local declaration of num2
i.e. int num2. 25
Register Variable
• Register variable uses register keyword before its
definitions and it is stored in the CPU register.
• It is a local variable.
• These variables have fast processing.
• The default value of the register variable is
garbage.
• Its lifetime is till the end of the function.
• Example:
register int a = 30;
26
Point to be noted
• Not every type of variable can be stored in a
CPU register.
• For example, if the microprocessor has 16-bit
registers then they cannot hold a float value or
a double value, which require 4 and 8 bytes
respectively.
• However, if you use the register storage class
for a float or a double variable, you won’t get
any error messages.
• All that would happen is the compiler would
treat the variables to be of auto storage class.
27
28
Extern Variable
• The extern variables use the extern keyword before the
variable definition.
• This enhances the variable visibility, and the variable can be
used in different C files.
• It is a global variable.
• It is not necessary to initialize the variable at the time of its
declaration.
• Default value is 0.
• Its lifetime is the entire program.
• Syntax:
– extern data_type variable_name;
• Example:
– extern int a;
01/18/2026 29
Extern in C
30
Extern in C
31
32
33
Declaration and Definition of Variables in C
• Declaration – Information to compiler about the variable
• Definition – Memory is allocated for the variable
according to type
• For all storage classes declaration and definition are same
except for extern
• When a keyword extern precedes, its only a declaration
and the compiler waits for definition without raising error
34
Constant Variable
• The constant variables are special variables in C
whose value does not change throughout the
program after initialization.
• It is a read-only variable in C.
• Initializing a constant variable at the time of
variable declaration is mandatory.
• It uses the “const” keyword before its definition.
35
Constant Variable
• Syntax:
– const data_type variable_name = initial_value;
• We can initialize other variables anytime and
anywhere in the program and can also change
their value.
• But constant variables have a fixed value,
you cannot change their value after their
initialization.
• If you try to change the value of a constant
variable, the program will generate errors.
36
#include <stdio.h>
int main()
{
int a = 30;
// declaring constant variable
const int b =10;
// Changing value of constant variable
b = 20;
return 0;
}
Output:
Error
37
Storage Classes in C
Four Storage Classes, classified based on life and scope of access:
Storage Life Scope
Class
Auto Created when a function is Can be accessed
called and destroyed only within the
automatically when function function where
exits it is declared
Default – int n;
Register Similar to Auto but stored in Similar to Auto
register, a faster memory.
Number of register is limited.
Therefore automatically
converted to auto if necessary
register int number;
38
Storage Classes in C
Storage Class Life Scope
Static • Created when a function Accessible only
is called inside the function
• Mandatory for where it is defined
programmer to initialize
• Lives till the program
executes
static int n =10;
Extern Created when the program Global variables
execution starts that can be shared
across files
extern int n;
39