7 votes 7 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 asked Jun 21, 2016 edited Jan 24 by makhdoom ghaya go_editor 6.6k views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
Best answer 15 votes 15 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 See all 13 13 Comments reply Show 10 previous comments shweta1920 commented Apr 16, 2017 reply Follow Share its a static variable .... and static variables use the same copy of varible for every function.... 0 votes 0 votes Harpreet0745 commented Aug 3, 2020 reply Follow Share 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 votes 0 votes gatecse commented Aug 3, 2020 reply Follow Share There's nothing like that in C. 0 votes 0 votes Please log in or register to add a comment.
2 votes 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 See all 0 reply Please log in or register to add a comment.
0 votes 0 votes 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 See all 0 reply Please log in or register to add a comment.