blogger Indonesia

Follow judhyns blog

RESOLVING VARIABLE REFERENCES

Introduction

When the same variable is resolved using both local definition and global definition, the local definition is given preference. This is called the rule of inheritance. It says that when you can resolve a reference to the variable by using multiple definitions, the nearest definition is given preference. Since local definition is the nearest, it gets preference.

Program

int i =0;            //Global variable  /A main() {     int i ;                 // local variable for main  / B     void f1(void) ;         //C     i =0;                   // D     printf("value of i in main %d\n",i);   // E     f1();                                  // F     printf("value of i after call%d\n",i); // G } void f1(void)                       // H {     int i=0;          //local variable for f1      // I     i = 50;                                // J } 

Explanation

  1. Here i is declared globally and locally in function main and in function f1, respectively, as given in statements A, B and I.

  2. Statement D refers to i, which can be resolved by using both local definition and global definition. Local definition is given more preference. So statement D refers to the definition at statement B and all the statements in main refer to the definition at statement B, that is, the local definition.

  3. When a function is called, statement i = 50 refers to the local definition in that function (definition at statement I).

  4. Using statement G, the value of i is 0 because both main and function f1 refer to their local copies of i. So the changed value of f1 is not reflected in main.

  5. Even if you comment local definition of function f1 at statement I the value printed remains the same. This is because main refers to its local copy while f1 refers to the global variable i — the two are different.

Point to Remember

When a variable can be resolved by using multiple references, the local definition is given more preference.

THE CONCEPT OF GLOBAL VARIABLES

Introduction
The various modules can share information by using global variables.

Program
#include
int i =0; //Global variable
main()
{
int j; // local variable in main
void f1(void) ;
i =0;
printf("value of i in main %d\n",i);
f1();
printf("value of i after call%d\n",i);
}
void f1(void)
{
int k; // local variable for f1.
i = 50;
}

Explanation
When you define a variable inside the function block it is called a local variable.

The local variable can be accessed only in the block in which it is declared.

j is the local variable for main and it can be accessed only in the block main. That means you cannot access it in function f1.

k is the local variable for function f1 and it cannot be accessed in main.

The variable i, which is outside main, is called a global variable. It can be accessed from function main as well as function f1.

Any expression in this function is going to operate on the same i.

When you call function f1, which sets the value of i to 50, it is also reflected in main because main and f1 are referring to the same variable, i.

Points to Remember
Global variables can be accessed in all the functions in that file.

Any update to the global variable also affects the other functions, because all functions refer to the same value of i.

When you want to share information between multiple functions, you can use the concept of global variables.

Folow me on

Popular Posts

feed

PR Check

activesearchresults

judhyn's blog

 

http://www.judhyn.blogspot.com | |