546 views
0 votes
0 votes

What is the output of this program ?Please give reason..

#include<stdio.h> 
int g() ; 
int f(); 
void main() 

printf(“%d”,f()); 

int g(){ 
int a=7,b=8,c; 

int a=3; 
b=6; 
c=b+a; 

return (c+b+a); 

int f() 

return (g()); 
}

2 Answers

0 votes
0 votes

If you see,from main f is called which calls g internally.So our focus is what g returns,same will be returned by f.

Rules:- The variables declared in the blocked ( braces ) will be local to that block and they will get priority if there is some other variable visible in that block with same name.

Inner block can access outer block variable but outer block cannot access inner block variable.

int g() 

{  
     int a=7,b=8,c;  // a,b,c are declared with in blue(color) braces block where a and b are initialized with integers and c is garbage
     {   
     int a=3;    // here a is local to the black(color) braces block where it is initialized to 3.
     b=6;       // this is assignment and not declaration and as there is no b in this block,so the outer block b will be                                                 accessed   and updated to 6.
     c=b+a;  //Same as above for c. Now b contains 6 and a contains 3. 6+3=9,will be assigned to c declared in outer block.
     }  
return (c+b+a);  // here c and b are updated by the above inner block,but remember the int a=3 was local to inner block,and outer block a is not affected and as this statement is in outer block it will use its own a with value as 7. 

Now final return is 9+6+7 =22 answer
}  

Related questions

0 votes
0 votes
0 answers
4
RiteshSingh asked Oct 9, 2018
509 views
Check whether the given language is regular or not:L={am bn ; nm<=789}