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
Natural Language Processing
PDF
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
PPT
Html Ppt
ODP
Genetic algorithm ppt
PPTX
Web design principles
PPTX
Genetic algorithm
PPTX
HTML Forms
Natural Language Processing
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
Html Ppt
Genetic algorithm ppt
Web design principles
Genetic algorithm
HTML Forms

What's hot (20)

PPTX
Control statements in c
PPTX
Storage class in C Language
PPT
Strings
PPTX
Functions in C
PPSX
Break and continue
PPTX
07. Virtual Functions
PPT
Operator Overloading
PDF
Operators in c programming
PPTX
Input output statement in C
PPT
Decision making and looping
PPTX
Decision Making Statement in C ppt
PPTX
PPTX
Static Data Members and Member Functions
PPT
User Defined Functions
PPTX
Polymorphism in c++(ppt)
PPTX
C programming - String
PPTX
Type casting in java
PPTX
Presentation on Function in C Programming
PPTX
classes and objects in C++
Control statements in c
Storage class in C Language
Strings
Functions in C
Break and continue
07. Virtual Functions
Operator Overloading
Operators in c programming
Input output statement in C
Decision making and looping
Decision Making Statement in C ppt
Static Data Members and Member Functions
User Defined Functions
Polymorphism in c++(ppt)
C programming - String
Type casting in java
Presentation on Function in C Programming
classes and objects in C++
Ad

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

PPT
Lecture 14 - Scope Rules
PPTX
Scope rules : local and global variables
DOCX
Programming Global variable
PPT
Functions in c
PPTX
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
DOC
C notes diploma-ee-3rd-sem
PPTX
LOCAL VARIABLES AND GLOBAL VARIABLES.pptx
PPTX
Storage classes in C
PDF
Chapter 11 Function
PPTX
Storage_classes_and_Scope_rules.pptx
PDF
Data structure scope of variables
PPTX
Unit 2 CMath behind coding.pptx
PPT
Lecture 13 - Storage Classes
DOCX
Report on Flutter.docx prasentation for study
DOC
Basic construction of c
Lecture 14 - Scope Rules
Scope rules : local and global variables
Programming Global variable
Functions in c
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
C notes diploma-ee-3rd-sem
LOCAL VARIABLES AND GLOBAL VARIABLES.pptx
Storage classes in C
Chapter 11 Function
Storage_classes_and_Scope_rules.pptx
Data structure scope of variables
Unit 2 CMath behind coding.pptx
Lecture 13 - Storage Classes
Report on Flutter.docx prasentation for study
Basic construction of c
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)

PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Pre independence Education in Inndia.pdf
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
PDF
Business Ethics Teaching Materials for college
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PDF
Open folder Downloads.pdf yes yes ges yes
PDF
English Language Teaching from Post-.pdf
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
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 Đ...
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Insiders guide to clinical Medicine.pdf
PDF
Module 3: Health Systems Tutorial Slides S2 2025
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Pre independence Education in Inndia.pdf
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
Business Ethics Teaching Materials for college
How to Manage Starshipit in Odoo 18 - Odoo Slides
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
Open folder Downloads.pdf yes yes ges yes
English Language Teaching from Post-.pdf
The Final Stretch: How to Release a Game and Not Die in the Process.
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
O7-L3 Supply Chain Operations - ICLT Program
Renaissance Architecture: A Journey from Faith to Humanism
Insiders guide to clinical Medicine.pdf
Module 3: Health Systems Tutorial Slides S2 2025
NOI Hackathon - Summer Edition - GreenThumber.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?

SPL 9 | Scope of Variables in C