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?
Tired of spending huge amount of money to become a Software professional?
Do an online course
@ baabtra.com
We put industry standards to practice. Our structured, activity based courses are so designed
to make a quick, good software professional out of anybody who holds a passion for coding.
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

PPTX
Algorithm Design & Implementation
PDF
Lecture 9
PPT
L01 intro-daa - ppt1

What's hot (18)

PPTX
Algorithm and flowchart2010
PDF
Algorithm defination, design & Implementation
PPTX
Algorithm and flowchart with pseudo code
PPTX
Flowcharts and algorithms
PPTX
Algorithm and Flowcharts
PPTX
FIT-Unit3 chapter 1 -computer program
PPTX
Algorithm and flowchart
PPSX
Algorithm and flowchart
DOCX
Which is not a step in the problem
PPT
Algorithms and flowcharts
PPTX
Type conversion, precedence, associativity in c programming
PPTX
Procedural programming
PPTX
phases of algorithm
PPTX
Introduction to computer science
PDF
How do you do that in FRP
PDF
Algorithm and c language
PPTX
flowchart & algorithms
Algorithm and flowchart2010
Algorithm defination, design & Implementation
Algorithm and flowchart with pseudo code
Flowcharts and algorithms
Algorithm and Flowcharts
FIT-Unit3 chapter 1 -computer program
Algorithm and flowchart
Algorithm and flowchart
Which is not a step in the problem
Algorithms and flowcharts
Type conversion, precedence, associativity in c programming
Procedural programming
phases of algorithm
Introduction to computer science
How do you do that in FRP
Algorithm and c language
flowchart & algorithms
Ad

Viewers also liked (7)

DOCX
Application of MoMSME- Support for Entrepreneurial and Management Development...
PDF
Baabtra.com and massbaab.com where are we heading-
PPTX
PDF
Acquiring new skills what you should know
Application of MoMSME- Support for Entrepreneurial and Management Development...
Baabtra.com and massbaab.com where are we heading-
Acquiring new skills what you should know
Ad

Similar to Scope of variables (20)

PDF
Data structure scope of variables
PDF
SPL 9 | Scope of Variables in C
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...
DOCX
Programming Global variable
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PDF
11 2. variable-scope rule,-storage_class
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PPT
Lecture 14 - Scope Rules
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PPTX
Scope rules : local and global variables
PPTX
Global variables, sorting static variables,function and arrays,
PDF
Latest C Interview Questions and Answers
PDF
C Programming Storage classes, Recursion
PPTX
LOCAL VARIABLES AND GLOBAL VARIABLES.pptx
Data structure scope of variables
SPL 9 | Scope of Variables in C
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...
Programming Global variable
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
11 2. variable-scope rule,-storage_class
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
Lecture 14 - Scope Rules
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
Scope rules : local and global variables
Global variables, sorting static variables,function and arrays,
Latest C Interview Questions and Answers
C Programming Storage classes, Recursion
LOCAL VARIABLES AND GLOBAL VARIABLES.pptx

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

PPTX
Agile methodology and scrum development
PDF
Baabtra.com programming at school
PDF
99LMS for Enterprises - LMS that you will love
PPTX
Chapter 6 database normalisation
PPTX
Chapter 5 transactions and dcl statements
PPTX
Chapter 4 functions, views, indexing
PPTX
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
PPTX
Chapter 1 introduction to sql server
PPTX
Chapter 1 introduction to sql server
Agile methodology and scrum development
Baabtra.com programming at school
99LMS for Enterprises - LMS that you will love
Chapter 6 database normalisation
Chapter 5 transactions and dcl statements
Chapter 4 functions, views, indexing
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 1 introduction to sql server
Chapter 1 introduction to sql server

Recently uploaded (20)

PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
KodekX | Application Modernization Development
PDF
Electronic commerce courselecture one. Pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Encapsulation theory and applications.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Diabetes mellitus diagnosis method based random forest with bat algorithm
Chapter 3 Spatial Domain Image Processing.pdf
KodekX | Application Modernization Development
Electronic commerce courselecture one. Pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
NewMind AI Monthly Chronicles - July 2025
Reach Out and Touch Someone: Haptics and Empathic Computing
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Dropbox Q2 2025 Financial Results & Investor Presentation
Digital-Transformation-Roadmap-for-Companies.pptx
Empathic Computing: Creating Shared Understanding
Encapsulation theory and applications.pdf
A Presentation on Artificial Intelligence
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
cuic standard and advanced reporting.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Per capita expenditure prediction using model stacking based on satellite ima...
The AUB Centre for AI in Media Proposal.docx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy

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? Tired of spending huge amount of money to become a Software professional? Do an online course @ baabtra.com We put industry standards to practice. Our structured, activity based courses are so designed to make a quick, good software professional out of anybody who holds a passion for coding.
  • 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: info@baabtra.com