SlideShare a Scribd company logo
1 of 16
GLOBAL VARIABLES
NAME-RAMANDEEP KAUR
ROLL NO.115305
INTRODUCTION TO GLOBAL
VARIABLES
VARIABLES
  VARIABLES ARE DECLARED INSIDE A BLOCK HAVE
  BLOCKSCOPE.BLOCK SCOPE VARIABLES CAN ONLY BE
  ACCESSED WITHIN THE BLOCK IN WHICH THE BLOCK THEY
  ARE DECLARED(OR A NESTED SUB-BLOCK),AND ARE
  DESTROYED WHEN THE BLOCK ENDS.
Variables declared outside of a
block are called global
variables. Global varaiables
have program scope,which
means they can be accessed
every where in the program and
they are destroyed when the
program ends.
Example
Ing_nx;//global variable
Int main( )
{
Int ny; //local variable ny

// global vars can be seen every where in program
//so we can change their values here
g_nx=5;
}//ny is destroyed here
Because global variables have
program scope,they can be
used across multiple files.In the
section on progaram with
multiple files,you learned that in
order to use a function declared
in another file,you have to use a
forward declaration,or a header
file.
Similarly,in order to use a global
variable that has been declared in
another file,you have to use a
forward declaration or a header
file,along with extern
keyword.Extern tells the computer
that you are not declaring a new
variable,but instead referring to a
variable declared else where.
Here is an example of using a
forward declaration style
extern:
Global.cpp:
//declaration of g_value
Int g n Value=5;

main.cpp
//extern tells the compiler this variable is
declared elsewhere
Extern int g _n Value;
Int main()
{
g_n Value=7;
return 0;
}
Here is an example of using a header file
extern:
global.cpp:
//declaration of g_nValue
Int g_nValue=5;
global.h:
#ifndef GLOBAL_H//header guards
#define GLOBAL_H

//extern tells the compiler this variable is
declared elsewhere
extern int g_value
# end if
main.cpp:
# include “global.h”
Int main()
{g_nValue=7;
Return 0;
}
Generally speaking,if a global
variable is going to be be
used in more than 2 files,it’s
better to use the header file
aapproach.Some
programmers place all of a
programs global variables in a
header file named globals.h
to be included by other .cpp
files that need to use them.
Local variables with the same
name as a global variables
hide the global variable inside
that block.However,the global
scope operator(::) can be
used to tell the compiler you
mean the global variable
version:
Int nValue=5
int main( )
{int n Value=7;//hides the
          global n Value
          variable
n Value++;//increments
          local n Value,not
          global nValue
:: nValue--;//decrements
             global nValue,
             not local
              nValue
return 0;
}//local nValue is
destroyed.
However,having local
variables with the sme
name as global variables is
usually a recipe for
trouble,and should be
avoided whenever
possible.
New programmers ae often
tempted to use lots of
global variables,because
they are easy to work
with,especially when many
functions are involved.

More Related Content

What's hot

Launching Applications
Launching ApplicationsLaunching Applications
Launching Applicationsprimeteacher32
 
Shell Shock
Shell ShockShell Shock
Shell ShockJie Liau
 
PyParis2017 / Tutorial transcript - Function-as-a-service : a pythonic perspe...
PyParis2017 / Tutorial transcript - Function-as-a-service : a pythonic perspe...PyParis2017 / Tutorial transcript - Function-as-a-service : a pythonic perspe...
PyParis2017 / Tutorial transcript - Function-as-a-service : a pythonic perspe...Pôle Systematic Paris-Region
 
C++ compilation process
C++ compilation processC++ compilation process
C++ compilation processRahul Jamwal
 
Types - slice, map, new, make, struct - Gopherlabs
Types - slice, map, new, make, struct - Gopherlabs Types - slice, map, new, make, struct - Gopherlabs
Types - slice, map, new, make, struct - Gopherlabs sangam biradar
 
Removal Of Recursion
Removal Of RecursionRemoval Of Recursion
Removal Of RecursionRicha Sharma
 
Pipeline interface
Pipeline interfacePipeline interface
Pipeline interfaceallanh0526
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.jsSu Zin Kyaw
 
Monitoring on Kubernetes using prometheus
Monitoring on Kubernetes using prometheusMonitoring on Kubernetes using prometheus
Monitoring on Kubernetes using prometheusChandresh Pancholi
 

What's hot (10)

Launching Applications
Launching ApplicationsLaunching Applications
Launching Applications
 
Shell Shock
Shell ShockShell Shock
Shell Shock
 
The Loops
The LoopsThe Loops
The Loops
 
PyParis2017 / Tutorial transcript - Function-as-a-service : a pythonic perspe...
PyParis2017 / Tutorial transcript - Function-as-a-service : a pythonic perspe...PyParis2017 / Tutorial transcript - Function-as-a-service : a pythonic perspe...
PyParis2017 / Tutorial transcript - Function-as-a-service : a pythonic perspe...
 
C++ compilation process
C++ compilation processC++ compilation process
C++ compilation process
 
Types - slice, map, new, make, struct - Gopherlabs
Types - slice, map, new, make, struct - Gopherlabs Types - slice, map, new, make, struct - Gopherlabs
Types - slice, map, new, make, struct - Gopherlabs
 
Removal Of Recursion
Removal Of RecursionRemoval Of Recursion
Removal Of Recursion
 
Pipeline interface
Pipeline interfacePipeline interface
Pipeline interface
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.js
 
Monitoring on Kubernetes using prometheus
Monitoring on Kubernetes using prometheusMonitoring on Kubernetes using prometheus
Monitoring on Kubernetes using prometheus
 

Similar to Global Variables: Introduction, Scope, and Usage

Storage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxStorage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxCheriviralaNikhil
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro headerhasan Mohammad
 
Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++Reddhi Basu
 
Lecture 8_Libraries.pptx
Lecture 8_Libraries.pptxLecture 8_Libraries.pptx
Lecture 8_Libraries.pptxNelyJay
 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and AnswersDaisyWatson5
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C Self employed
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 FunctionDeepak Singh
 
04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptxManas40552
 
What is Closure and Its Uses in PHP
 What is Closure and Its Uses in PHP What is Closure and Its Uses in PHP
What is Closure and Its Uses in PHPAndolasoft Inc
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of VariableMOHIT DADU
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptAmanuelZewdie4
 
Advanced C Programming Notes
Advanced C Programming NotesAdvanced C Programming Notes
Advanced C Programming NotesLeslie Schulte
 

Similar to Global Variables: Introduction, Scope, and Usage (20)

Storage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxStorage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptx
 
Functions
FunctionsFunctions
Functions
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro header
 
Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Lecture 13 - Storage Classes
Lecture 13 - Storage ClassesLecture 13 - Storage Classes
Lecture 13 - Storage Classes
 
Lecture 8_Libraries.pptx
Lecture 8_Libraries.pptxLecture 8_Libraries.pptx
Lecture 8_Libraries.pptx
 
Node js Global Packages
Node js Global PackagesNode js Global Packages
Node js Global Packages
 
SPL 9 | Scope of Variables in C
SPL 9 | Scope of Variables in CSPL 9 | Scope of Variables in C
SPL 9 | Scope of Variables in C
 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and Answers
 
Chapter-3 Scoping.pptx
Chapter-3 Scoping.pptxChapter-3 Scoping.pptx
Chapter-3 Scoping.pptx
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
 
Storage classes
Storage classesStorage classes
Storage classes
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
 
What is Closure and Its Uses in PHP
 What is Closure and Its Uses in PHP What is Closure and Its Uses in PHP
What is Closure and Its Uses in PHP
 
Scope of Variables.pptx
Scope of Variables.pptxScope of Variables.pptx
Scope of Variables.pptx
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of Variable
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
 
Advanced C Programming Notes
Advanced C Programming NotesAdvanced C Programming Notes
Advanced C Programming Notes
 

Recently uploaded

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 

Global Variables: Introduction, Scope, and Usage

  • 3. VARIABLES VARIABLES ARE DECLARED INSIDE A BLOCK HAVE BLOCKSCOPE.BLOCK SCOPE VARIABLES CAN ONLY BE ACCESSED WITHIN THE BLOCK IN WHICH THE BLOCK THEY ARE DECLARED(OR A NESTED SUB-BLOCK),AND ARE DESTROYED WHEN THE BLOCK ENDS.
  • 4. Variables declared outside of a block are called global variables. Global varaiables have program scope,which means they can be accessed every where in the program and they are destroyed when the program ends.
  • 5. Example Ing_nx;//global variable Int main( ) { Int ny; //local variable ny // global vars can be seen every where in program //so we can change their values here g_nx=5; }//ny is destroyed here
  • 6. Because global variables have program scope,they can be used across multiple files.In the section on progaram with multiple files,you learned that in order to use a function declared in another file,you have to use a forward declaration,or a header file.
  • 7. Similarly,in order to use a global variable that has been declared in another file,you have to use a forward declaration or a header file,along with extern keyword.Extern tells the computer that you are not declaring a new variable,but instead referring to a variable declared else where.
  • 8. Here is an example of using a forward declaration style extern: Global.cpp: //declaration of g_value Int g n Value=5; main.cpp //extern tells the compiler this variable is declared elsewhere Extern int g _n Value;
  • 9. Int main() { g_n Value=7; return 0; } Here is an example of using a header file extern: global.cpp: //declaration of g_nValue Int g_nValue=5; global.h:
  • 10. #ifndef GLOBAL_H//header guards #define GLOBAL_H //extern tells the compiler this variable is declared elsewhere extern int g_value # end if main.cpp: # include “global.h” Int main() {g_nValue=7; Return 0; }
  • 11. Generally speaking,if a global variable is going to be be used in more than 2 files,it’s better to use the header file aapproach.Some programmers place all of a programs global variables in a header file named globals.h to be included by other .cpp files that need to use them.
  • 12. Local variables with the same name as a global variables hide the global variable inside that block.However,the global scope operator(::) can be used to tell the compiler you mean the global variable version:
  • 13. Int nValue=5 int main( ) {int n Value=7;//hides the global n Value variable n Value++;//increments local n Value,not global nValue
  • 14. :: nValue--;//decrements global nValue, not local nValue return 0; }//local nValue is destroyed.
  • 15. However,having local variables with the sme name as global variables is usually a recipe for trouble,and should be avoided whenever possible.
  • 16. New programmers ae often tempted to use lots of global variables,because they are easy to work with,especially when many functions are involved.