Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Scope Rule & Storage Class
2Local Variables Local variable– Variables declared inside a function– Accessible only from the function in which it is d...
3Local Variables Example#include <stdio.h>void function() ;void main(void){int i = 1, j = 0;function() ;printf(“main : i=...
4Local Variables Example#include <stdio.h>void function() ;void main(void){int i = 1, j = 0;function() ;printf(“main : i=...
5Local Variables Parameters of a function– The parameters of a function are local variable of the function#include <stdio...
6Global Variables Global variable– Variables declared outside functions• When the program starts, global variables are cr...
7Global Variables Example#include <stdio.h>int g = 0;int main(void){int i =0, j=0 ;...}void function(int i){int j = 3 ;g+...
8Global Variables Example#include <stdio.h>int g = 0;int main(void){int i =0, j=0 ;function(i) ;printf(“main : %d %d %dn”...
9Static Variables Static variable– Variables declared with static– Similar to global variables• When the program starts, ...
10Static Variables Examplevoid func(void){static int s ;int k ;printf( “%d %dn”, k, ++s ) ;k = 10 ;printf( “%d %dn”, k, +...
11Static Variables Example: How many times the function iscalled?#include <stdio.h>#include <stdlib.h>#include <time.h>vo...
12Register Variables Register variable– Request to use registers which are the most fastest memory– Declarable as only in...
13Const Const– Using to declare variables that cannot be modified– When it is declared, must initialize it– Not a Constan...
14Scope Rules Scope Rules– Identifiers declared inside blocks• Accessible only from the block in which it is declared• Wh...
15Scope Rules Scope Rules#include <stdio.h>int g ;void function() {static int a ;printf(“a=%d g=%d”, a, g);}int main(void...
Upcoming SlideShare
Loading in …5
×

11 2. variable-scope rule,-storage_class

621 views

Published on

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

  • Be the first to like this

11 2. variable-scope rule,-storage_class

  1. 1. Scope Rule & Storage Class
  2. 2. 2Local Variables Local variable– Variables declared inside a function– Accessible only from the function in which it is declared– When a function starts, the variables in the function iscreated, and when the function ends the variables aredestroyed• If the function executes again, new memory is assigned to thevariable. It’s value is not continuedint sum( int k ) {int a ;a = k + 3 ;return a ;}
  3. 3. 3Local Variables Example#include <stdio.h>void function() ;void main(void){int i = 1, j = 0;function() ;printf(“main : i=%d, j=%d n”, i, j);}void function(){int j=0, k = 1;printf(“in function : j=%d, k=%d n”, j, k);}The same variablesorDifferent variables with the same name?
  4. 4. 4Local Variables Example#include <stdio.h>void function() ;void main(void){int i = 1, j = 0;function() ;printf(“main : i=%d, j=%d n”, i, j);}void function(){int j=0, k = 1;printf(“in function : j=%d, k=%d n”, j, k);i = 0 ;}i jmainj kfunction
  5. 5. 5Local Variables Parameters of a function– The parameters of a function are local variable of the function#include <stdio.h>void function(int) ;int main(void){int i = 0, j = 0;function(i, j) ;printf(“i=%d, j=%d n”, i, j);}void function(int i, int j){int k = 0 ;i++ ; j++ ;printf(“i=%d, j=%d, k=%d n”, i, j, k);}i jmaini jfunctionk
  6. 6. 6Global Variables Global variable– Variables declared outside functions• When the program starts, global variables are created.• When the program ends, global variables are destroyed– Accessible at any functions– Automatically initialized with 0int g ;int main(){printf( “%dn”, g ) ;my_func() ;printf( “%dn”, g ) ;}void my_func(){g = 10 ;}
  7. 7. 7Global Variables Example#include <stdio.h>int g = 0;int main(void){int i =0, j=0 ;...}void function(int i){int j = 3 ;g++ ;}i jmaini jfunctiongyour program
  8. 8. 8Global Variables Example#include <stdio.h>int g = 0;int main(void){int i =0, j=0 ;function(i) ;printf(“main : %d %d %dn”, i, j, g);}void function(int i){int j = 3 ;i++ ;printf(“in function : %d %d %dn”, i, j, g);g++ ;}i jmaini jfunctiongyour program
  9. 9. 9Static Variables Static variable– Variables declared with static– Similar to global variables• When the program starts, static variables are created• When the program ends, static variables are destroyed• Initialized to 0, automatically– Static variables declared inside a function• Accessible only from the function in which it is declared– Static variables declared outside functions• Accessible only from the same source file in which it is declared(Later, refer to making program by many source files)
  10. 10. 10Static Variables Examplevoid func(void){static int s ;int k ;printf( “%d %dn”, k, ++s ) ;k = 10 ;printf( “%d %dn”, k, ++s ) ;}#include <stdio.h>void func(void)void main(void){func() ;func() ;}
  11. 11. 11Static Variables Example: How many times the function iscalled?#include <stdio.h>#include <stdlib.h>#include <time.h>void do_nothing(void) ;void main(void){int j ;srand( time(NULL) ) ;for( j = 0 ; j < 10 ; j++ )if( rand() % 2 ) do_nothing() ;}void do_nothing(void){static int cnt ;printf( “do_nothing is called %d time(s)n”, ++cnt ) ;}
  12. 12. 12Register Variables Register variable– Request to use registers which are the most fastest memory– Declarable as only integer data type– Not always allocated to the registers• Even though you declare register variables, it may not beallocated into registers.[Ex]void main() {register int i; /* same to register i */for(i = 0; i < LIMIT; ++i) {…}}
  13. 13. 13Const Const– Using to declare variables that cannot be modified– When it is declared, must initialize it– Not a Constant !! It is a Variable.[Ex] const int a = 1;[Ex] const int a = 1;a = 4; /* error!! */a++; /* error!! */
  14. 14. 14Scope Rules Scope Rules– Identifiers declared inside blocks• Accessible only from the block in which it is declared• When the block starts, the variables are available, and whenthe block ends, the variables die– Identifiers declared outside block• Accessible from all blocks• If the program starts, the variables are available, and when theprogram ends, the variables die
  15. 15. 15Scope Rules Scope Rules#include <stdio.h>int g ;void function() {static int a ;printf(“a=%d g=%d”, a, g);}int main(void) {int a = 3, b = 4;{int b=5;printf(“%d %d”, a, b, g);}printf(“%d %d”, a, b, g);function() ;}When each variable isinitialized and destroyed?Which variables are printed?

×