in Programming in C
589 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()); 
}

in Programming in C
by
589 views

1 comment

o/p is 22??
0
0

2 Answers

0 votes
0 votes

optput=22

edited by

1 comment

Thanks, buddy ...But it gives an error when u compile and run on ide and i cant understand why .Please check it.
0
0
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
}  

4 Comments

reference:https://ideone.com/5RVo7U

I compile on DEVcpp ,ideone and both give the same errors .

error1:    6:11: error: expected primary-expression before '%' token printf(“%d”,f());

error2:   6:12: error:   'd' was not declared in this scope printf(“%d”,f());

0
0
ahh.. thats because you might have copied from some website and the double quotes from that website are converted to some other character when you did copy paste.From the print f remove the double quotes surrounding %d and then add them again by from your keyboard(using double quote key) :)
1
1
Ohh my bad, I ditto copied from gradeup .Thanks a lot.
0
0

Related questions