Scope Rules
#include <stdio.h>
The scope of a variable in C is the block int main()
or the region in the program where a {
variable is declared, defined, and used. int var = 34;
printf("%d", var);
return 0;
}
void func()
Outside this region, we cannot access the {
variable, and it is treated as an undeclared printf("%d", var);
identifier. }
31/10/2025 Programming for Problem Solving Week 6 1
Local, Global And Static Variables
Local Variables:
The variables that are defined within the body of a function or a block, are local to that
function block only and are called local variables.
Global Variables:
The variables that are defined outside any function are called global variables. All
functions in the program can access and modify global variables. It is useful to declare a
variable global if it is to be used by many functions in the program. Global variables are
automatically initialized to 0 at the time of declaration.
31/10/2025 Programming for Problem Solving Week 6 2
Local, Global And Static Variables
Static Variables:
Static variables are declared by writing keyword static in front of the declaration.
Syntax:
static type var_name;
A static variable is initialized only once, and the value of a static variable is retained
between function calls. If a static variable is not initialized, then it is automatically
initialized to 0.
31/10/2025 Programming for Problem Solving Week 6 3
Local, Global And Static Variables
#include<stdio.h>
#include <stdio.h> void func();
void mul(int, int); int main()
int a = 10; // a is global variable {
int main() func();
{ func();
int b = 20, c=30; // b and c are local variables func();
printf("Sum = %d", a+b); }
mul(b, c); void func()
return 0; {
} int a=10;
void mul(int x, int y) // x and y are local variables static int b=20;
{ printf("a = %d b = %d\n", a, b);
printf(“Product = %d", x * y); a++;
} b++;
}
31/10/2025 Programming for Problem Solving Week 6 4
Storage Classes
C Storage Classes are used to describe the features of a variable/function.
These features basically include the scope, visibility, and lifetime which
help us to trace the existence of a particular variable during the runtime of
a program.
C language uses 4 storage classes, namely:
1. auto
2. extern
3. static
4. register
31/10/2025 Programming for Problem Solving Week 6 5
Storage Classes
i) Auto:
It is also known as the automatic storage class, and it acts as the default storage class
for all the variables that are local in nature.
We use the keyword auto to define the automatic variables, but its optional.
31/10/2025 Programming for Problem Solving Week 6 6
Storage Classes
Features of automatic variables:
The scope of an automatic variable is limited to that block in which we are
defining them.
The visibility of these variables is also limited to that block in which we are
defining them.
The initialization of these variables is, by default, a garbage value.
The memory that is assigned to an automatic variable gets free when it exits from
a block.
31/10/2025 Programming for Problem Solving Week 6 7
Storage Classes
ii) Extern:
The extern or external storage class is used to give a reference of a global variable that
is visible to ALL the program files.
When you use 'extern', the variable cannot be initialized. However, it points the variable
name at a storage location that has been defined.
The extern modifier is most used when there are two or more files sharing the same
global variables or functions.
An external integral type’s default initial value is going to be 0, or else it is null.
31/10/2025 Programming for Problem Solving Week 6 8
Storage Classes
iii) Static:
This type of storage class gives an instruction to a compiler to keep the given local
variable around during the program’s lifetime- instead of creating it and then destroying
it every time it comes into a scope and goes out of it.
Thus, when we make a local variable static, it allows the variable to maintain the values
that are available between various function calls.
31/10/2025 Programming for Problem Solving Week 6 9
Storage Classes
Features of static variables:
Those variables that we define as static specifiers can hold their assigned value
that exists between various function calls.
The static local variables are only visible to the block or the function in which we
have defined them.
The initial value of a static variable, by default, is 0.
The visibility of any static global variable stays limited to that file in which we
have declared it.
31/10/2025 Programming for Problem Solving Week 6 10
Storage Classes
iv) Registers:
We use the register storage class for defining the local variables that must be stored in any
register, and not in a RAM.
It means that the maximum size of this variable is equal to that of the register size (it is
usually one word).
Also, we cannot apply the ‘&’ unary operator to it because it has no memory location.
We must only use the register in the case of those variables which require quick access.
31/10/2025 Programming for Problem Solving Week 6 11
Storage Classes
Features of register variables:
Its access time is comparatively much faster than that of the automatic variables.
One cannot dereference a register variable. In other words, one cannot make use of
the ‘&’ operator in the case of a register variable.
The default initial value of any given register local value will always be garbage.
31/10/2025 Programming for Problem Solving Week 6 12
Storage Classes
Reference link: https://www.geeksforgeeks.org/storage-classes-in-c/
31/10/2025 Programming for Problem Solving Week 6 13
Storage Classes
#include <stdio.h> }
void display();
extern int a; int a;
int main()
{ void display()
auto int b; {
register int c; static int d;
printf("auto variable b = %d\n", b); printf("Static variable d = %d\n", d);
printf("global variable a = %d\n", a); d++;
printf("register variable c = %d\n\n", c); }
display();
display();
display();
31/10/2025 Programming for Problem Solving Week 6 14