9 9 votes The following program main() { inc(); inc(); inc(); } inc() { static int x; printf("%d", ++x); } prints 012 prints 123 prints 3 consecutive, but unpredictable numbers prints 111 Programming in C isro2015 programming-in-c functions + – go_editor 8.9k views answer comment Share Follow Print 0 reply Please log in or register to add a comment.
Best answer 16 16 votes answer is B) As static is implicitly initialized to 0 and inc() is called 3 times hence output will be 1,2,3 Kapil answered Jun 21, 2016 • selected Jun 23, 2016 by Arjun Kapil comment Share Follow See all 13 Comments 13 13 Comments reply Show 10 previous comments shweta1920 commented Apr 16, 2017 reply Follow flag its a static variable .... and static variables use the same copy of varible for every function.... 0 0 replyShare Harpreet0745 commented Aug 3, 2020 reply Follow flag isn't it true that it is not recommended to do increment or decrement in printf statement as doing this would lead to a compiler dependent output?? therefore option C is true; or the Static variable is creating any difference here 0 0 replyShare gatecse commented Aug 3, 2020 reply Follow flag There's nothing like that in C. 0 0 replyShare Please log in or register to add a comment.
2 2 votes Answer (B) x is static so during initialization it's value is 0,with every call it increases by 1 first call = 0+1 =1 second call= 1+1=2 third call = 2+1 = 3 so 123 is printed Regina Phalange answered Apr 1, 2017 Regina Phalange comment Share Follow 0 reply Please log in or register to add a comment.
1 1 vote Important points about the static variables:- They're initialised right at the start, and their value are stored inside the Runtime Environment. Once initialised, they're never initialised again. If in the beginning, value is not assigned for initialisation, then it is assigned 0 by default. static int x; Value assigned 0 implicitly. So, x = 0. Combine this fact with pre-increment, we get option B JashanArora answered Dec 7, 2019 JashanArora comment Share Follow 0 reply Please log in or register to add a comment.