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
- Here i is declared globally and locally in function main and in function f1, respectively, as given in statements A, B and I.
- 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.
- When a function is called, statement i = 50 refers to the local definition in that function (definition at statement I).
- 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.
- 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.
No comments:
Post a Comment