Wednesday, January 24, 2018

Scope Control in C programming

In this topic, we will discuss the scope of a variable in C programming, more specifically we will see the behavior of Global and Local variables. In C programming variables are separated by their accessibility. There are some rules/limitation in using variables.


Local Variable

Variables that are declared inside a function or block are called Local variable for that function.

Let's see an example



#include <stdio.h>

int main() 
{
    int n; // n is a local variable to main() function
    return 0;
}


void function_01() 
{
   int n1; // n1 is a local variable to function_01() function
}


void function_02() 
{
   int n2; // n2 is a local variable to function_02() function
}


The following chart will show the accessibility of variables
Function Name Accessible Variable
main n
function_01 n1
function_02 n2


Global Variable

Variables that are declared outside of all functions are called Global variable.

Let's see an example



#include <stdio.h>

int x; // x is a global variable.

int main() 
{
    int n; // n is a local variable to main() function
    return 0;
}

void function_01() 
{
   int n1; // n1 is a local variable to function_01() function
}

The following chart will show the accessibility of variables
Function Name Accessible Variable
main n, x
function_01 n1, x


Important

Whenever we call any function with parameters that function just receive the value of their corresponding parameters. Remember function never receive any variable (same placeholder) directly just receive the values and creates temporary variables to store those values ( which are the local variables of that function ).

Let's see an example



#include <stdio.h>

int x = 10; /* global variable */

void display(int a, int b)
{
    printf ("value of a = %d, b = %d \n", a, b);

    int sum = x + a + b; // sum = 10 + 20 + 30
    printf ("sum = %d \n", sum);
}


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

    /* Variable initialization */
    a = x + 10; // a = 10 + 10 = 20
    b = x + 20; // b = 10 + 20 = 30

    display(a, b); /* calling a void function */

    return 0;
}

Note
  • In line 23 we are calling a void function with parameter (a, b). here we are not passing the exact variable directly, we just passing their corresponding value (20, 30)
  • In our main function we have variables a and b also in our display function we have another two temporary variables named a and b (we can also use different names). Though their names and values are same they are not same. Like in every city there are many people who have the same name.
  • In this program x is the only global variable so we can use it both main and display function


Let’s see another Example to understand above points



#include <stdio.h>

int x = 10; /* global variable */

void display(int a1, int b1)
{
    /* a1 and b1 is the local variable to this function. */
    /* a1 holds the value of a and b1 holds the value of b*/
    printf ("value of a = %d, b = %d \n", a1, b1);
    printf ("value of x = %d \n", x);
}

void changeValue(int a, int b)
{
    /* changing the value of all variable */
    a = 1;
    b = 2;
    x = 3;
}


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

    /* Variable initialization */
    a = 100;
    b = 200;
    x = x + 10; // x = 10 + 10;


    display(a, b); /* calling a void function */


    changeValue(a, b); /* calling changeValue function with parameters (100, 200) */

    /* printing all the variable accessible from main() function */
    printf("\nprinting all the variable accessible from main() function\n");

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

    return 0;
}


Note
  • In line # we pass the value only not the variable.



Previous Page

Next Page



No comments:

Post a Comment