SlideShare a Scribd company logo
1 of 13
Sreyachandran.v
Sreya.chandran31@gmail.com
www.facebook.com/sreyachandran
twitter.com/username
in.linkedin.com/in/profilename
Scope of variable 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
Scope of variable
•Refers to where variables can be referenced
•There are three places where variables can be declared in c
programming language
1. Local variable
2. Global variable
3. Formal parameters
Local variable
• 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.
function_1()
{
data_type a;
}
• Example
#include<stdio.h>
main()
{
int a=5;
++a;
printf("%d", a);
increment();
return 0;
}
void increment()
{
++a;
printf("%d", a);
}
Error : function is trying to access
local variable of main()
Global variable
• Global variables in c have their declaration outside
the function definition of all functions used within the
program.
• They remains in the memory as long as the program
is executing.
• Example
#include<stdio.h>
int a=5;
main()
{
++a;
printf("%dn", a);
increment();
return 0;
}
void increment()
{
++a;
printf("%dn",a);
}
Output
6
7
Nested scope
• Local variables of same name can be nested inside global
variables.
int total = 5;
int main ( )
{
int total = 4;
….
}
int sub1 ( ) {
int i = total;
}
Nested scope
• Example
#include<stdio.h>
int f1();
int a=10;
main()
{
int a=20;
int b=f1();
printf(“%dn % d”, a , b);
}
f1(int x)
{
x=a;
return x;
}
Using local variable ‘a’
Using global variable ‘a’
Output
20
10
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
Give a feedback @ massbaab.com/baabtra
Thanks in advance
www.baabtra.com | www.massbaab.com |www.baabte.com
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
Contact Us

More Related Content

What's hot

Software Design Engineer 1
Software Design Engineer 1Software Design Engineer 1
Software Design Engineer 1
chalikars
 

What's hot (18)

Templates c++ - prashant odhavani - 160920107003
Templates   c++ - prashant odhavani - 160920107003Templates   c++ - prashant odhavani - 160920107003
Templates c++ - prashant odhavani - 160920107003
 
Variable Scope
Variable ScopeVariable Scope
Variable Scope
 
scope of python
scope of pythonscope of python
scope of python
 
C++
C++C++
C++
 
C aptitude book
C aptitude bookC aptitude book
C aptitude book
 
Programming Methodology
Programming MethodologyProgramming Methodology
Programming Methodology
 
Ict question
Ict questionIct question
Ict question
 
F# for BLOBA, by brandon d'imperio
F# for BLOBA, by brandon d'imperioF# for BLOBA, by brandon d'imperio
F# for BLOBA, by brandon d'imperio
 
Software Design Engineer 1
Software Design Engineer 1Software Design Engineer 1
Software Design Engineer 1
 
A quick introduction to c programming
A quick introduction to c programmingA quick introduction to c programming
A quick introduction to c programming
 
Death to project documentation with eXtreme Programming
Death to project documentation with eXtreme ProgrammingDeath to project documentation with eXtreme Programming
Death to project documentation with eXtreme Programming
 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing Techniques
 
Lecture1
Lecture1Lecture1
Lecture1
 
Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++
 
Fun with lambda expressions
Fun with lambda expressionsFun with lambda expressions
Fun with lambda expressions
 
Me2011 presentation by Manfred Jeusfeld
Me2011 presentation by Manfred JeusfeldMe2011 presentation by Manfred Jeusfeld
Me2011 presentation by Manfred Jeusfeld
 
Procedural programming
Procedural programmingProcedural programming
Procedural programming
 
Programming Methodology
Programming MethodologyProgramming Methodology
Programming Methodology
 

Viewers also liked

Viewers also liked (8)

Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Storage classes
Storage classesStorage classes
Storage classes
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 

Similar to Scope of variables

Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
Alpana Gupta
 

Similar to Scope of variables (20)

scope of variables
scope of variablesscope of variables
scope of variables
 
Scope of variables
Scope of variablesScope of variables
Scope of variables
 
Scope of variables
Scope of variablesScope of variables
Scope of variables
 
Scope of variables
Scope of variablesScope of variables
Scope of variables
 
Presentation
PresentationPresentation
Presentation
 
Functions using stack and heap
Functions using stack and heapFunctions using stack and heap
Functions using stack and heap
 
Function creation and function call in c
Function creation and function call in c  Function creation and function call in c
Function creation and function call in c
 
Function creation and function call in c copy
Function creation and function call in c   copyFunction creation and function call in c   copy
Function creation and function call in c copy
 
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
 
C programming
C programmingC programming
C programming
 
CP c++ programing project Unit 1 intro.pdf
CP c++ programing project  Unit 1 intro.pdfCP c++ programing project  Unit 1 intro.pdf
CP c++ programing project Unit 1 intro.pdf
 
Cegonsoft Franchise Opportunity
Cegonsoft Franchise OpportunityCegonsoft Franchise Opportunity
Cegonsoft Franchise Opportunity
 
Python Developer Resume | Python Developer Skill Set, Jobs, Roles and Respons...
Python Developer Resume | Python Developer Skill Set, Jobs, Roles and Respons...Python Developer Resume | Python Developer Skill Set, Jobs, Roles and Respons...
Python Developer Resume | Python Developer Skill Set, Jobs, Roles and Respons...
 
Fundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptxFundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptx
 
C++ programming Online Training in Hyderabad
C++ programming Online Training in HyderabadC++ programming Online Training in Hyderabad
C++ programming Online Training in Hyderabad
 
C programming
C programmingC programming
C programming
 
U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).ppt
 
C programming
C programmingC programming
C programming
 
Python Programming Essentials - M17 - Functions
Python Programming Essentials - M17 - FunctionsPython Programming Essentials - M17 - Functions
Python Programming Essentials - M17 - Functions
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 

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

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

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 
Baabtra soft skills
Baabtra soft skillsBaabtra soft skills
Baabtra soft skills
 
Cell phone jammer
Cell phone jammerCell phone jammer
Cell phone jammer
 
Apple iwatches
Apple iwatchesApple iwatches
Apple iwatches
 
Driverless car
Driverless carDriverless car
Driverless car
 
Brain computer interface(neethu,bincy,sanooja)
Brain computer interface(neethu,bincy,sanooja)Brain computer interface(neethu,bincy,sanooja)
Brain computer interface(neethu,bincy,sanooja)
 
Chapter 5 : How To Program
Chapter  5 : How To ProgramChapter  5 : How To Program
Chapter 5 : How To Program
 
Chapter 5 : How To Program?
Chapter  5 : How To Program?Chapter  5 : How To Program?
Chapter 5 : How To Program?
 

Recently uploaded

Recently uploaded (20)

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
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 future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
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...
 
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
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 

Scope of variables

  • 1.
  • 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. Scope of variable •Refers to where variables can be referenced •There are three places where variables can be declared in c programming language 1. Local variable 2. Global variable 3. Formal parameters
  • 5. Local variable • 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. function_1() { data_type a; }
  • 6. • Example #include<stdio.h> main() { int a=5; ++a; printf("%d", a); increment(); return 0; } void increment() { ++a; printf("%d", a); } Error : function is trying to access local variable of main()
  • 7. Global variable • Global variables in c have their declaration outside the function definition of all functions used within the program. • They remains in the memory as long as the program is executing.
  • 8. • Example #include<stdio.h> int a=5; main() { ++a; printf("%dn", a); increment(); return 0; } void increment() { ++a; printf("%dn",a); } Output 6 7
  • 9. Nested scope • Local variables of same name can be nested inside global variables. int total = 5; int main ( ) { int total = 4; …. } int sub1 ( ) { int i = total; } Nested scope
  • 10. • Example #include<stdio.h> int f1(); int a=10; main() { int a=20; int b=f1(); printf(“%dn % d”, a , b); } f1(int x) { x=a; return x; } Using local variable ‘a’ Using global variable ‘a’ Output 20 10
  • 11. 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.
  • 12. 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 Give a feedback @ massbaab.com/baabtra Thanks in advance www.baabtra.com | www.massbaab.com |www.baabte.com
  • 13. 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 Contact Us