SlideShare a Scribd company logo
Scope of variables
Anu
anusdhar4@gmail.com
www.facebook.com/AnuSasidharan
twitter.com/Anu
in.linkedin.com/in/Anu
9400892764
SCOPE OF VARIABLES IN C
Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
•Region of the program where a defined variable can
have its existence .
•3 places where variables can be declared in C
programming language:
 Inside a function or a block -local variables
 Outside of all functions - global variables
 In the definition of function parameters-formal parameters.
What Actually Scope Of A Variable Mean??
What are local variables ???
•It exists only inside the specific function that creates them.
•They are unknown to other functions and to the main
program.
•Local variables cease to exist once the function that created
them is completed.
•They are recreated each time a function is executed or
called.
EXAMPLE
int sum(int a, int b)
{
int tot;
tot=a+b;
}
•These variables can be accessed by any function
comprising the program.
•They do not get recreated if the function is recalled.
What are global variables
???
EXAMPLE
#include<stdio.h>
int sum(int, int);
int tot;
main()
{
int a=10,b=5;
tot=sum(a,b);
}
sum(int c, int d)
{
tot=c+d;
return tot;
}
#include <stdio.h>
int sqr( int x ) ;
main()
{
int i , j ;
sqr( i ) ;
printf( "%d %dn", i, j ) ;
}
int sqr( int x )
{
j = x * x ; }
SIMPLE EXAMPLE ILLUSTRATING
SCOPE OF VARIABLES
CORRECT METHOD
#include <stdio.h>
int sqr( int x ) ;
main()
{
int i , j ;
sqr( i ) ;
printf( "%d %dn", i, j ) ;
}
int sqr( int x )
{ int j;
j= x * x ; }
LOCAL VARIABLES Vs
GLOBAL VARIABLES
Using local variables allows to focus attention on smaller more
manageable pieces of the program. Globals forces to keep track
of how they're being used throughout the entire system.
While working with other people, you must coordinate who's
creating which global variables. It is not valid to create two
globals of the same name.
The effects of algorithm steps reduces the cases of unexpected
behavior.
Localizing the effects of algorithm steps makes the program
easier for others to understand.
 If the two functions need to communicate, then we
generally need global variables.
If the entire program is built around some central data
and if access to that data is needed in nearly every
function and particularly if that is a large data structure,
then it makes sense to make such data global.
When do we use global variables ???
A Final Implementation with an Example
#include <stdio.h>
int f1( void ) ;
int f2( int x, int a ) ;
int a ;
main()
{ int a, b, c ;
a = 7 ;
b = f1() ;
c = f2( a, b ) ;
printf( "%dn %dn %dn", a, b, c ) ;
}
int f1( void )
{ a = 12 ;
printf( "%dn ", a ) ; return( a + 5 ) ;
}
int f2( int x, int a )
{ printf( "%dn ", a ) ; return( x * a ) ;
}
OUTPUT
12
17
7
17
119
a=7,12
a=7,12,7
b=17
x=7
a=7,12,7,17
c=119
CONCLUSION
Scope of variable was explained. It defines the existence or
the availability of variables in a program.
Types of scope was discussed. Scope depends upon the type
of variable declaration.
Global and Local variables were briefly explained.
Want to learn more about programming or Looking to
become a good programmer? Are you wasting time on searching
so many contents online? Do you want to learn things quickly? Do
an online course @ baabtra.com. We provide development, testing
and production environments just like in the industry to practice.
The courses are so structured to make a quick, good software
professional.
Follow us @ twitter.com/baabtra
Like us @ facebook.com/baabtra
Subscribe to us @ youtube.com/baabtra
Become a follower @ slideshare.net/BaabtraMentoringPartner
Connect to us @ in.linkedin.com/in/baabtra
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Cafit Square,
Hilite Business Park,
Near Pantheerankavu,
Kozhikode
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com

More Related Content

What's hot (20)

PPTX
Algorithm and flowchart
Elizabeth de Leon Aler
 
PPTX
FIT-Unit3 chapter 1 -computer program
raksharao
 
PPTX
Flowcharts and algorithms
Student
 
PPTX
Algorithm and flowchart
Rabin BK
 
PPTX
Chapter 6 algorithms and flow charts
Praveen M Jigajinni
 
PPSX
Algorithm and flowchart
Sachin Goyani
 
PDF
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
Muhammad Hammad Waseem
 
PPTX
C Operators
Yash Modi
 
PPTX
Algorithm and pseudocode conventions
saranyatdr
 
PPTX
Flowcharting and Algorithm
Zeinna Belle Desamito
 
PPT
Csc 130 class 2 problem analysis and flow charts(2)
Puneet narula
 
PPTX
Type conversion, precedence, associativity in c programming
Dhrumil Panchal
 
PPTX
connecting discrete mathematics and software engineering
Ram Kumar K R
 
PPTX
Flowchart and algorithem
ehsanullah786
 
PPT
3 algorithm-and-flowchart
Rohit Shrivastava
 
PPTX
Algorithms and flowcharts
Samuel Igbanogu
 
PPTX
Part17 radio button using vb.net 2012
Girija Muscut
 
PPTX
Part18 checkbox using vb.net
Girija Muscut
 
PPTX
Flowchart and algorithm
DHANIK VIKRANT
 
Algorithm and flowchart
Elizabeth de Leon Aler
 
FIT-Unit3 chapter 1 -computer program
raksharao
 
Flowcharts and algorithms
Student
 
Algorithm and flowchart
Rabin BK
 
Chapter 6 algorithms and flow charts
Praveen M Jigajinni
 
Algorithm and flowchart
Sachin Goyani
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
Muhammad Hammad Waseem
 
C Operators
Yash Modi
 
Algorithm and pseudocode conventions
saranyatdr
 
Flowcharting and Algorithm
Zeinna Belle Desamito
 
Csc 130 class 2 problem analysis and flow charts(2)
Puneet narula
 
Type conversion, precedence, associativity in c programming
Dhrumil Panchal
 
connecting discrete mathematics and software engineering
Ram Kumar K R
 
Flowchart and algorithem
ehsanullah786
 
3 algorithm-and-flowchart
Rohit Shrivastava
 
Algorithms and flowcharts
Samuel Igbanogu
 
Part17 radio button using vb.net 2012
Girija Muscut
 
Part18 checkbox using vb.net
Girija Muscut
 
Flowchart and algorithm
DHANIK VIKRANT
 

Viewers also liked (19)

Ad

Similar to Scope of variables (20)

PDF
Data structure scope of variables
Saurav Kumar
 
PDF
SPL 9 | Scope of Variables in C
Mohammad Imam Hossain
 
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
sekemioxiel
 
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
lablegtaton
 
PDF
11 2. variable-scope rule,-storage_class
웅식 전
 
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
zeltuprvth360
 
DOCX
Programming Global variable
imtiazalijoono
 
PPTX
Scope rules : local and global variables
sangrampatil81
 
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
botiodosseh
 
PPT
Lecture 14 - Scope Rules
Md. Imran Hossain Showrov
 
PDF
C Programming Storage classes, Recursion
Sreedhar Chowdam
 
PPTX
Global variables, sorting static variables,function and arrays,
Ahmad55ali
 
PPTX
LOCAL VARIABLES AND GLOBAL VARIABLES.pptx
atulsahu5689
 
Data structure scope of variables
Saurav Kumar
 
SPL 9 | Scope of Variables in C
Mohammad Imam Hossain
 
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
sekemioxiel
 
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
lablegtaton
 
11 2. variable-scope rule,-storage_class
웅식 전
 
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
zeltuprvth360
 
Programming Global variable
imtiazalijoono
 
Scope rules : local and global variables
sangrampatil81
 
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
botiodosseh
 
Lecture 14 - Scope Rules
Md. Imran Hossain Showrov
 
C Programming Storage classes, Recursion
Sreedhar Chowdam
 
Global variables, sorting static variables,function and arrays,
Ahmad55ali
 
LOCAL VARIABLES AND GLOBAL VARIABLES.pptx
atulsahu5689
 
Ad

More from baabtra.com - No. 1 supplier of quality freshers (20)

PPTX
Agile methodology and scrum development
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Acquiring new skills what you should know
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Baabtra.com programming at school
baabtra.com - No. 1 supplier of quality freshers
 
PDF
99LMS for Enterprises - LMS that you will love
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 4 functions, views, indexing
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 3 stored procedures
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Agile methodology and scrum development
baabtra.com - No. 1 supplier of quality freshers
 
Acquiring new skills what you should know
baabtra.com - No. 1 supplier of quality freshers
 
Baabtra.com programming at school
baabtra.com - No. 1 supplier of quality freshers
 
99LMS for Enterprises - LMS that you will love
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 4 functions, views, indexing
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 

Recently uploaded (20)

PDF
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
PDF
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
PPTX
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
PDF
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
PDF
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
DOCX
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
PPSX
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PPTX
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
PDF
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
PDF
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
PDF
Next level data operations using Power Automate magic
Andries den Haan
 
PDF
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
PPTX
Smart Factory Monitoring IIoT in Machine and Production Operations.pptx
Rejig Digital
 
PDF
Kubernetes - Architecture & Components.pdf
geethak285
 
PDF
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
Next level data operations using Power Automate magic
Andries den Haan
 
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
Smart Factory Monitoring IIoT in Machine and Production Operations.pptx
Rejig Digital
 
Kubernetes - Architecture & Components.pdf
geethak285
 
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 

Scope of variables

  • 3. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  • 4. •Region of the program where a defined variable can have its existence . •3 places where variables can be declared in C programming language:  Inside a function or a block -local variables  Outside of all functions - global variables  In the definition of function parameters-formal parameters. What Actually Scope Of A Variable Mean??
  • 5. What are local variables ??? •It exists only inside the specific function that creates them. •They are unknown to other functions and to the main program. •Local variables cease to exist once the function that created them is completed. •They are recreated each time a function is executed or called.
  • 6. EXAMPLE int sum(int a, int b) { int tot; tot=a+b; }
  • 7. •These variables can be accessed by any function comprising the program. •They do not get recreated if the function is recalled. What are global variables ???
  • 8. EXAMPLE #include<stdio.h> int sum(int, int); int tot; main() { int a=10,b=5; tot=sum(a,b); } sum(int c, int d) { tot=c+d; return tot; }
  • 9. #include <stdio.h> int sqr( int x ) ; main() { int i , j ; sqr( i ) ; printf( "%d %dn", i, j ) ; } int sqr( int x ) { j = x * x ; } SIMPLE EXAMPLE ILLUSTRATING SCOPE OF VARIABLES
  • 10. CORRECT METHOD #include <stdio.h> int sqr( int x ) ; main() { int i , j ; sqr( i ) ; printf( "%d %dn", i, j ) ; } int sqr( int x ) { int j; j= x * x ; }
  • 11. LOCAL VARIABLES Vs GLOBAL VARIABLES Using local variables allows to focus attention on smaller more manageable pieces of the program. Globals forces to keep track of how they're being used throughout the entire system. While working with other people, you must coordinate who's creating which global variables. It is not valid to create two globals of the same name. The effects of algorithm steps reduces the cases of unexpected behavior. Localizing the effects of algorithm steps makes the program easier for others to understand.
  • 12.  If the two functions need to communicate, then we generally need global variables. If the entire program is built around some central data and if access to that data is needed in nearly every function and particularly if that is a large data structure, then it makes sense to make such data global. When do we use global variables ???
  • 13. A Final Implementation with an Example #include <stdio.h> int f1( void ) ; int f2( int x, int a ) ; int a ; main() { int a, b, c ; a = 7 ; b = f1() ; c = f2( a, b ) ; printf( "%dn %dn %dn", a, b, c ) ; } int f1( void ) { a = 12 ; printf( "%dn ", a ) ; return( a + 5 ) ; } int f2( int x, int a ) { printf( "%dn ", a ) ; return( x * a ) ; } OUTPUT 12 17 7 17 119 a=7,12 a=7,12,7 b=17 x=7 a=7,12,7,17 c=119
  • 14. CONCLUSION Scope of variable was explained. It defines the existence or the availability of variables in a program. Types of scope was discussed. Scope depends upon the type of variable declaration. Global and Local variables were briefly explained.
  • 15. Want to learn more about programming or Looking to become a good programmer? Are you wasting time on searching so many contents online? Do you want to learn things quickly? Do an online course @ baabtra.com. We provide development, testing and production environments just like in the industry to practice. The courses are so structured to make a quick, good software professional.
  • 16. Follow us @ twitter.com/baabtra Like us @ facebook.com/baabtra Subscribe to us @ youtube.com/baabtra Become a follower @ slideshare.net/BaabtraMentoringPartner Connect to us @ in.linkedin.com/in/baabtra Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 17. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Cafit Square, Hilite Business Park, Near Pantheerankavu, Kozhikode Start up Village Eranakulam, Kerala, India. Email: [email protected]