SlideShare ist ein Scribd-Unternehmen logo
1 von 17
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

Weitere ähnliche Inhalte

Was ist angesagt?

Which is not a step in the problem
Which is not a step in the problemWhich is not a step in the problem
Which is not a step in the problem
kasguest
 
flowchart & algorithms
flowchart & algorithmsflowchart & algorithms
flowchart & algorithms
Student
 

Was ist angesagt? (18)

Algorithm and flowchart2010
Algorithm and flowchart2010Algorithm and flowchart2010
Algorithm and flowchart2010
 
Algorithm defination, design & Implementation
Algorithm defination, design & ImplementationAlgorithm defination, design & Implementation
Algorithm defination, design & Implementation
 
Algorithm and flowchart with pseudo code
Algorithm and flowchart with pseudo codeAlgorithm and flowchart with pseudo code
Algorithm and flowchart with pseudo code
 
Flowcharts and algorithms
Flowcharts and algorithmsFlowcharts and algorithms
Flowcharts and algorithms
 
Algorithm and Flowcharts
Algorithm and FlowchartsAlgorithm and Flowcharts
Algorithm and Flowcharts
 
FIT-Unit3 chapter 1 -computer program
FIT-Unit3 chapter 1 -computer programFIT-Unit3 chapter 1 -computer program
FIT-Unit3 chapter 1 -computer program
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 
Which is not a step in the problem
Which is not a step in the problemWhich is not a step in the problem
Which is not a step in the problem
 
Algorithms and flowcharts
Algorithms and flowchartsAlgorithms and flowcharts
Algorithms and flowcharts
 
Algorithms & flowcharts
Algorithms & flowchartsAlgorithms & flowcharts
Algorithms & flowcharts
 
Type conversion, precedence, associativity in c programming
Type conversion, precedence, associativity in c programmingType conversion, precedence, associativity in c programming
Type conversion, precedence, associativity in c programming
 
Procedural programming
Procedural programmingProcedural programming
Procedural programming
 
phases of algorithm
phases of algorithmphases of algorithm
phases of algorithm
 
Introduction to computer science
Introduction to computer scienceIntroduction to computer science
Introduction to computer science
 
How do you do that in FRP
How do you do that in FRPHow do you do that in FRP
How do you do that in FRP
 
Algorithm and c language
Algorithm and c languageAlgorithm and c language
Algorithm and c language
 
flowchart & algorithms
flowchart & algorithmsflowchart & algorithms
flowchart & algorithms
 

Andere mochten auch (7)

Application of MoMSME- Support for Entrepreneurial and Management Development...
Application of MoMSME- Support for Entrepreneurial and Management Development...Application of MoMSME- Support for Entrepreneurial and Management Development...
Application of MoMSME- Support for Entrepreneurial and Management Development...
 
Jquery
JqueryJquery
Jquery
 
Baabtra.com and massbaab.com where are we heading-
Baabtra.com and massbaab.com   where are we heading-Baabtra.com and massbaab.com   where are we heading-
Baabtra.com and massbaab.com where are we heading-
 
Html
HtmlHtml
Html
 
5g
5g5g
5g
 
Stored procedures with cursor
Stored procedures with cursorStored procedures with cursor
Stored procedures with cursor
 
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
 

Ähnlich wie Scope of variables

379008-rc217-functionalprogramming
379008-rc217-functionalprogramming379008-rc217-functionalprogramming
379008-rc217-functionalprogramming
Luis Atencio
 
object oriented programming part inheritance.pptx
object oriented programming part inheritance.pptxobject oriented programming part inheritance.pptx
object oriented programming part inheritance.pptx
urvashipundir04
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
Deepak Singh
 

Ähnlich wie Scope of variables (20)

Procedural programming
Procedural programmingProcedural programming
Procedural programming
 
Scope of variables
Scope of variablesScope of variables
Scope of variables
 
Book management system
Book management systemBook management system
Book management system
 
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 notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semC notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-sem
 
Monolithic and Procedural Programming
Monolithic and Procedural ProgrammingMonolithic and Procedural Programming
Monolithic and Procedural Programming
 
Basic construction of c
Basic construction of cBasic construction of c
Basic construction of c
 
scope of variables
scope of variablesscope of variables
scope of variables
 
379008-rc217-functionalprogramming
379008-rc217-functionalprogramming379008-rc217-functionalprogramming
379008-rc217-functionalprogramming
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignment
 
Lecture 14 - Scope Rules
Lecture 14 - Scope RulesLecture 14 - Scope Rules
Lecture 14 - Scope Rules
 
object oriented programming part inheritance.pptx
object oriented programming part inheritance.pptxobject oriented programming part inheritance.pptx
object oriented programming part inheritance.pptx
 
Password protected diary
Password protected diaryPassword protected diary
Password protected diary
 
Contact management system
Contact management systemContact management system
Contact management system
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 
Functions
FunctionsFunctions
Functions
 
program development and paradigms
program development and paradigmsprogram development and paradigms
program development and paradigms
 
C functions list
C functions listC functions list
C functions list
 
Functional programming in TypeScript
Functional programming in TypeScriptFunctional programming in TypeScript
Functional programming in TypeScript
 

Mehr von baabtra.com - No. 1 supplier of quality freshers

Mehr von 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
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
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
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
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
 
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
 

Kürzlich hochgeladen

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
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
vu2urc
 

Kürzlich hochgeladen (20)

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
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
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
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
[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
 
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...
 
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...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
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
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
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...
 
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
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - 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. •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