Scope of variables


Published on

Published in: Technology, Travel
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Scope of variables

  1. 1. Anu 9400892764 SCOPE OF VARIABLES IN C
  2. 2. 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
  3. 3. •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??
  4. 4. 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.
  5. 5. EXAMPLE int sum(int a, int b) { int tot; tot=a+b; }
  6. 6. •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 ???
  7. 7. 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; }
  8. 8. #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
  9. 9. 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 ; }
  10. 10. 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.
  11. 11.  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 ???
  12. 12. 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
  13. 13. 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.
  14. 14. 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 @ 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.
  15. 15. Follow us @ Like us @ Subscribe to us @ Become a follower @ Connect to us @ Thanks in advance. | |
  16. 16. 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: