Page 81 - 6437
P. 81

Scope Rules

                          A scope in any programming is a region of the program where a defined variable can have
                   its  existence  and  beyond  that  variable  it  cannot  be  accessed.  There  are  three  places  where
                   variables can be declared in C programming language:
                                       Inside a function or a block which is called local variables,
                                       Outside of all functions which is called global variables.
                                       In the definition of function parameters which are called formal
                          parameters.
                          Let us understand what are local and global variables, and formal parameters.

                          Local Variables
                          Variables that are declar

                          ed  inside  a  function  or  block  are  called  local  variables.  They  can  be  used  only  by
                   statements  that  are  inside  that  function  or  block  of  code.  Local  variables  are  not  known  to
                   functions outside their own. The following example shows how local variables are used. Here all
                   the variables a, b, and c are local to main() function.

                 #include <stdio.h>


                 int main ()
                 {

                   /* local variable declaration */
                   int a, b;

                   int c;


                   /* actual initialization */
                   a = 10;

                   b = 20;
                   c = a + b;


                   printf ("value of a = %d, b = %d and c = %d\n", a, b, c);


                   return 0;





















                                                                                                      84
   76   77   78   79   80   81   82   83   84   85   86