1 1 vote Question: Programming in C + – mohitbawankar 2.2k views answer comment Share Follow Print See all 9 Comments 9 9 Comments reply gauravkc commented Jan 6, 2018 reply Follow flag 15 20 15 15 I think something is fuzzy about the scope here This one I get it I guess as when second time print1 is called, the statement will be skipped at runtime and so is assigning value 10 is skipped. Hence, 15 20. In print2, when second time is called, only static int x will get skipped but not x=10. Hence, 15 15 I have a doubt though, in this program, I typed print1(), print2(), print1(), print2() and to distinguish, in print2 I typed x=50. Output is 15 55 20 55 when print1 is called again, declaration is skipped, then it should simply add 5 and show 60. How is it 20? Is x different for both print1 and print2? 0 0 replyShare gauravkc commented Jan 6, 2018 reply Follow flag Have a look at addresses 0 0 replyShare mohitbawankar commented Jan 6, 2018 reply Follow flag yes it will print 15 55 20 55 0 0 replyShare mohitbawankar commented Jan 6, 2018 reply Follow flag please copy your code post here .. 0 0 replyShare gauravkc commented Jan 6, 2018 reply Follow flag It means that both x is declared once but in both functions. Both function have their own copy of one time declared x variable. 0 0 replyShare gauravkc commented Jan 6, 2018 reply Follow flag #include<stdio.h> void print1() { static int x=10; x+=5; printf("x=%d and the address in print1 is %p \n",x,&x); } void print2() { static int x; x=50; x+=5; printf("x=%d and the address in print2 is %p \n",x,&x); } int main() { print1(); print2(); print1(); print2(); } 0 0 replyShare mohitbawankar commented Jan 6, 2018 reply Follow flag see my comments its given by made easy 0 0 replyShare gauravkc commented Jan 6, 2018 reply Follow flag But that doesn't explain the case in my code. Then it should be 15 55 60 55 Also, addresses are different for both. 0 0 replyShare gauravkc commented Jan 6, 2018 reply Follow flag @joshi_nitish @Anu007 @hs_yadav @Ashwin Kulkarni Even if variable name is same, with multiple static declarations in different functions, is the scope of variable limited to repeated calls for same function only? 0 0 replyShare Please log in or register to add a comment.
0 0 votes I Think This will make you more clear.... Ans is ;- 15 20 15 15 Manis answered Jan 6, 2018 • edited Jan 6, 2018 by Manis Manis comment Share Follow See all 2 Comments 2 2 Comments reply mohitbawankar commented Jan 6, 2018 reply Follow flag ans is 15 20 15 15 0 0 replyShare mohitbawankar commented Jan 6, 2018 reply Follow flag Solution : (d) • print 1( ): x = 10 + 5 = 15; since the variable is of static storage class, hence it will retain its value between different function calls. • print 1( ): x = 15 + 5 = 20; since it has retained its value 15. • print 2( ): x is defined again inside the function and hence will print, x = x + 5 = 10 + 5 = 15. Again when the function will be called, x = 10 + 5 = 15. Here second time also x = 10 will be there because it is not initialized at the time of definition. Hence output 15, 20, 15, 15. 0 0 replyShare Please log in or register to add a comment.