Monday, 25 November 2013

Scope Rules In C

A scope in any program is a that region of the program where a defined variable can have its existence and beyond that variable can't be accessed. The three places where variables can be declared in C programming language:
 
C Programming
C Programming

Local Variables:

Variables which  are declared within a function or block are called local variables. They can be used only by statements which are inside that function.Local Variables are not more alive outside the functions .Here is the example of local variables. All the variables a, b and c are local Variables  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;
}

Global Variables:

Global variables are defined outside of all function, used in a program.Global Variables are usually on top of the program. The global variables  can be accessed inside any of the functions.You can access them form any where .

Example of global variable is:

#include <stdio.h>

/* global variable declaration */
int g;

int main ()
{
  /* local variable declaration */
  int a, b;

  /* actual initialization */
  a = 10;
  b = 20;
  g = a + b;

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

  return 0;
}
A same name will be assigned to a local and global variables in a same C program but value of local variable inside a function will take preference.

#include <stdio.h>

/* global variable declaration */
int g = 20;

int main ()
{
  /* local variable declaration */
  int g = 10;

  printf ("value of g = %d\n",  g);

  return 0;
}
Output:

value of g = 10

Formal Parameters:

Function parameters, formal parameters, are treated as local variables inside that function and they will take preference over the global variables.
example:

#include <stdio.h>

/* global variable declaration */
int a = 20;

int main ()
{
  /* local variables in main function */
  int x= 10;
  int y = 20;
  int z = 0;

  printf ("value of x in main = %d\n",  x);
  c = sum( x, y);
  printf ("value of z in main = %d\n",  z);

  return 0;
}

/* function to add two integers */
int sum(int x, int y)
{
    printf ("value of x in sum= %d\n",  x);
    printf ("value of y in sum= %d\n",  y);

    return x + y;
}
 result:

value of a in main() = 10
value of a in sum() = 10
value of b in sum() = 20
value of c in main() = 30

Initializing Local and Global Variables:

Local variable  is not initialized by the system, you must initialize it by yourself. Global variables are initialized the system.

Data Type            Initial Default Value
int                                 0
char                            '\0'
float                              0
double                          0
pointer                         NULL

It I good to habit of initialize the variables otherwise in some cases you will get the unexpected results.
C - Overview
C - Basic Syntax
C - Data Types
C - Variables
C - Constants
C - Storage Classes
C - Operators
C - Decision Making
C - Loops
C - Functions
C - Scope Rules
C - Arrays
C - Pointers
C - Strings
C - Structures
C - Unions
C - Bit Fields
C - Typedef
C - Input & Output
C - File I/O
C - Preprocessors
C - Header Files
C - Type Casting
C - Error Handling
C - Recursion
C - Variable Arguments
C - Memory Management
C - Command Line Arguments 


0 comments:

Post a Comment