SlideShare a Scribd company logo
Structured Programming Language
Scope of a variable
Mohammad Imam Hossain,
Lecturer, CSE, UIU
Scope of a variable
A scope in any programming is a region of
the program where a defined variable can
have its existence and beyond that variable
it can’t be accessed.
Three places to declare variable:
1. inside a function or block (local
variables)
2. outside of all the functions (global
variables)
3. in the definition of function
parameters (formal parameters)
Local Variables
• Variables that are declared inside a function or block
are called local variables.
• They can be used only by statements that are inside
that function or block of code.
• Local variables are not known to functions outside
their own.
Sample Program#include<stdio.h>
int main()
{
int a=10;
return 0;
}
Sample Program#include<stdio.h>
int main()
{
int a=10;
if(1)
{
int b=a/2;
}
return 0;
}
Sample Program#include<stdio.h>
int main()
{
int a=10;
if(1)
{
int b=a/2;
printf("%d %dn",a,b); ///10 5
}
return 0;
}
Sample Program#include<stdio.h>
int main()
{
int a=10;
if(1)
{
int b=a/2;
printf("%d %dn",a,b); ///10 5
if(1)
{
int c=a+b;
}
}
return 0;
}
Sample Program#include<stdio.h>
int main()
{
int a=10;
if(1)
{
int b=a/2;
printf("%d %dn",a,b); ///10 5
if(1)
{
int c=a+b;
printf("%d %d %dn",a,b,c); ///10 5 15
}
}
return 0;
}
Sample Program#include<stdio.h>
int main()
{
int a=10;
if(1)
{
int b=a/2;
printf("%d %dn",a,b); ///10 5
if(1)
{
int c=a+b;
printf("%d %d %dn",a,b,c); ///10 5 15
b=c;
a=b*2;
}
}
return 0;
}
Sample Program#include<stdio.h>
int main()
{
int a=10;
if(1)
{
int b=a/2;
printf("%d %dn",a,b); ///10 5
if(1)
{
int c=a+b;
printf("%d %d %dn",a,b,c); ///10 5 15
b=c;
a=b*2;
}
printf("%dn",c); ///error
printf("%d %dn",b,a); ///15 30
}
return 0;
}
Sample Program#include<stdio.h>
int main()
{
int a=10;
if(1)
{
int b=a/2;
printf("%d %dn",a,b); ///10 5
if(1)
{
int c=a+b;
printf("%d %d %dn",a,b,c); ///10 5 15
b=c;
a=b*2;
}
printf("%dn",c); ///error
printf("%d %dn",b,a); ///15 30
}
b=a*b; ///error
printf("%d %dn",c,b); ///error
printf("%dn",a); ///30
return 0;
}
Global Variables
• Global variables are defined outside a function,
usually on top of the program.
• Global variables hold their values throughout the
lifetime of your program.
• A global variable can be accessed by any function i.e.
a global variable is available for use throughout the
entire program after its declaration.
Sample Program
#include<stdio.h>
int g=0; ///global variable
int main()
{
return 0;
}
Sample Program
#include<stdio.h>
int g=0; ///global variable
int main()
{
printf("%dn",g); ///0
return 0;
}
Sample Program
#include<stdio.h>
int g=0; ///global variable
int main()
{
printf("%dn",g); ///0
int i;
for(i=1;i<=5;i++){
}
return 0;
}
Sample Program
#include<stdio.h>
int g=0; ///global variable
int main()
{
printf("%dn",g); ///0
int i;
for(i=1;i<=5;i++){
int j=i;
printf("%dn",j);
g=g+j;
}
return 0;
}
Sample Program
#include<stdio.h>
int g=0; ///global variable
int main()
{
printf("%dn",g); ///0
int i;
for(i=1;i<=5;i++){
int j=i;
printf("%dn",j);
g=g+j;
}
printf("%dn",j); ///error
printf("%d %dn",i,g); ///6 15
return 0;
}
Special Case
• A program can have same name for variables within
different scope.
• A program can have same name for local and global
variables but the value of local variable inside a
function will take preference.
Sample Program
#include<stdio.h>
int g=100; ///global variable
int main()
{
return 0;
}
Sample Program
#include<stdio.h>
int g=100; ///global variable
int main()
{
printf("%dn",g); ///100
return 0;
}
Sample Program
#include<stdio.h>
int g=100; ///global variable
int main()
{
printf("%dn",g); ///100
int g=50;
return 0;
}
Sample Program
#include<stdio.h>
int g=100; ///global variable
int main()
{
printf("%dn",g); ///100
int g=50;
printf("%dn",g); ///50
if(1){
}
return 0;
}
Sample Program
#include<stdio.h>
int g=100; ///global variable
int main()
{
printf("%dn",g); ///100
int g=50;
printf("%dn",g); ///50
if(1){
g=10;
}
return 0;
}
Sample Program
#include<stdio.h>
int g=100; ///global variable
int main()
{
printf("%dn",g); ///100
int g=50;
printf("%dn",g); ///50
if(1){
g=10;
int g=20;
}
return 0;
}
Sample Program
#include<stdio.h>
int g=100; ///global variable
int main()
{
printf("%dn",g); ///100
int g=50;
printf("%dn",g); ///50
if(1){
g=10;
int g=20;
printf("%dn",g); ///20
}
return 0;
}
Sample Program
#include<stdio.h>
int g=100; ///global variable
int main()
{
printf("%dn",g); ///100
int g=50;
printf("%dn",g); ///50
if(1){
g=10;
int g=20;
printf("%dn",g); ///20
g=5;
}
return 0;
}
Sample Program
#include<stdio.h>
int g=100; ///global variable
int main()
{
printf("%dn",g); ///100
int g=50;
printf("%dn",g); ///50
if(1){
g=10;
int g=20;
printf("%dn",g); ///20
g=5;
}
printf("%dn",g); ///10
return 0;
}
Reference
• https://www.tutorialspoint.com/cprogramming/c_sc
ope_rules.htm

More Related Content

PDF
Modular Programming in C
PPTX
Functions in c
PPTX
Modular programming
PPTX
Storage classes in C
PPTX
Structures and Unions
PPTX
Pointers and Dynamic Memory Allocation
PPTX
Scope rules : local and global variables
Modular Programming in C
Functions in c
Modular programming
Storage classes in C
Structures and Unions
Pointers and Dynamic Memory Allocation
Scope rules : local and global variables

What's hot (20)

PPT
Decision making and branching
PPTX
Control statements in c
PPT
RECURSION IN C
PPTX
Strings in C language
PPT
Input and output in C++
PPT
Variables in C Programming
PDF
Object oriented programming c++
PPTX
Control and conditional statements
PPTX
Loops in C Programming Language
PPTX
Decision Making Statement in C ppt
PPTX
C formatted and unformatted input and output constructs
PPTX
Functions in C
PPTX
Dynamic memory allocation in c
PPTX
Introduction to Selection control structures in C++
PPT
Structure of a C program
PPSX
Break and continue
PPTX
C programming - String
PPTX
The Loops
PPTX
Methods in java
Decision making and branching
Control statements in c
RECURSION IN C
Strings in C language
Input and output in C++
Variables in C Programming
Object oriented programming c++
Control and conditional statements
Loops in C Programming Language
Decision Making Statement in C ppt
C formatted and unformatted input and output constructs
Functions in C
Dynamic memory allocation in c
Introduction to Selection control structures in C++
Structure of a C program
Break and continue
C programming - String
The Loops
Methods in java
Ad

Similar to SPL 9 | Scope of Variables in C (20)

DOCX
Programming Global variable
PPT
Lecture 14 - Scope Rules
PDF
Data structure scope of variables
PDF
11 2. variable-scope rule,-storage_class
PPTX
LOCAL VARIABLES AND GLOBAL VARIABLES.pptx
PDF
C Programming Storage classes, Recursion
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PPT
Functions in c
PPT
storage clas
Programming Global variable
Lecture 14 - Scope Rules
Data structure scope of variables
11 2. variable-scope rule,-storage_class
LOCAL VARIABLES AND GLOBAL VARIABLES.pptx
C Programming Storage classes, Recursion
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
Functions in c
storage clas
Ad

More from Mohammad Imam Hossain (20)

PDF
DS & Algo 6 - Offline Assignment 6
PDF
DS & Algo 6 - Dynamic Programming
PDF
DS & Algo 5 - Disjoint Set and MST
PDF
DS & Algo 4 - Graph and Shortest Path Search
PDF
DS & Algo 3 - Offline Assignment 3
PDF
DS & Algo 3 - Divide and Conquer
PDF
DS & Algo 2 - Offline Assignment 2
PDF
DS & Algo 2 - Recursion
PDF
DS & Algo 1 - Offline Assignment 1
PDF
DS & Algo 1 - C++ and STL Introduction
PDF
DBMS 1 | Introduction to DBMS
PDF
DBMS 10 | Database Transactions
PDF
DBMS 3 | ER Diagram to Relational Schema
PDF
DBMS 2 | Entity Relationship Model
PDF
DBMS 7 | Relational Query Language
PDF
DBMS 4 | MySQL - DDL & DML Commands
PDF
DBMS 5 | MySQL Practice List - HR Schema
PDF
TOC 10 | Turing Machine
PDF
TOC 9 | Pushdown Automata
PDF
TOC 8 | Derivation, Parse Tree & Ambiguity Check
DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Dynamic Programming
DS & Algo 5 - Disjoint Set and MST
DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Divide and Conquer
DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Recursion
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - C++ and STL Introduction
DBMS 1 | Introduction to DBMS
DBMS 10 | Database Transactions
DBMS 3 | ER Diagram to Relational Schema
DBMS 2 | Entity Relationship Model
DBMS 7 | Relational Query Language
DBMS 4 | MySQL - DDL & DML Commands
DBMS 5 | MySQL Practice List - HR Schema
TOC 10 | Turing Machine
TOC 9 | Pushdown Automata
TOC 8 | Derivation, Parse Tree & Ambiguity Check

Recently uploaded (20)

PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
master seminar digital applications in india
PDF
Business Ethics Teaching Materials for college
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
Cell Types and Its function , kingdom of life
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
01-Introduction-to-Information-Management.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Pre independence Education in Inndia.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
master seminar digital applications in india
Business Ethics Teaching Materials for college
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Anesthesia in Laparoscopic Surgery in India
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Supply Chain Operations Speaking Notes -ICLT Program
TR - Agricultural Crops Production NC III.pdf
Cell Structure & Organelles in detailed.
Cell Types and Its function , kingdom of life
Final Presentation General Medicine 03-08-2024.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
human mycosis Human fungal infections are called human mycosis..pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
01-Introduction-to-Information-Management.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
O7-L3 Supply Chain Operations - ICLT Program
Pre independence Education in Inndia.pdf

SPL 9 | Scope of Variables in C