Wednesday, April 6, 2016

Variable Scope and Life Time



Variable Scope :

  • The region in which a variable is valid.
  • Variables declared outside of a function have global scope 



Variable Redefinitions:
  • You could re-declare any variable in your outer scope but you couldn't declare 2 variables with the same name in the same scope 

Example 1 : 




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
   #include <stdio.h>

   int nmax=20;

   int main ( ){
   int a=0 , b=1 , c , n ;
   printf("%d:  %d\n",1,a);
   printf("%d:  %d\n",2,b);
   int nmax=10;
   for (n=3 ; n<=nmax ; n++) {
    c=a+b;
    a=b;
    b=c;
    printf("%3d : %d\n",n,c);
   }

   return 0 ;
   }
;


Output :  1:  0       2:  1
       3 : 1
       4 : 2
       5 : 3
       6 : 5
       7 : 8
       8 : 13
       9 : 21
       10 : 34









Share this

0 Comment to "Variable Scope and Life Time"

Post a Comment