View
50
Download
0
Embed Size (px)
Citation preview
Anu
www.facebook.com/Anu Sasidharan
twitter.com/Anu
in.linkedin.com/in/Anu
9400892764
SCOPE OF VARIABLES IN C
Disclaimer: This presentation is prepared by trainees ofbaabtra as a part of mentoring program. This is not officialdocument of baabtra –Mentoring PartnerBaabtra-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 %d\n", 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 %d\n", 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( "%d\n %d\n %d\n", a, b, c ) ;
}int f1( void ){ a = 12 ;
printf( "%d\n ", a ) ; return( a + 5 ) ; } int f2( int x, int a ) { printf( "%d\n ", a ) ; return( x * a ) ; }
OUTPUT1217717119
a=7,12a=7,12,7b=17x=7a=7,12,7,17c=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 tobecome a good programmer? Are you wasting time on searchingso many contents online? Do you want to learn things quickly? Doan online course @ baabtra.com. We provide development, testingand production environments just like in the industry to practice.The courses are so structured to make a quick, good softwareprofessional.
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 StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
Cafit Square,Hilite Business Park,Near Pantheerankavu,Kozhikode
Start up VillageEranakulam,Kerala, India.
Email: [email protected]