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.7k 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 Don't you worry commented Jul 12, 2016 reply Follow Share why is it picking up next value as 1 for the next increment function .Why it is not again starting with 0? please explain. 0 votes 0 votes Kapil commented Jul 12, 2016 reply Follow Share when the control goes to inc() function, static is implicitly initialized default to 0 printf says ++x , so first increment will be there ,hence x = 1, will be printed.... 0 votes 0 votes Don't you worry commented Jul 12, 2016 reply Follow Share I meant to ask in function main we have 3 inc() function rt? when 1 inc() will be called then increment will happen in value of x and it will become 1 .its done here now it turn for 2nd inc () which is going t be called by fn main now i am asking why this new function call not considering initial value as 0 and againn incrementing to 1 if we do like this o/p will be 111 .....this is i want you to explain i understood you statement about static initialization .... 0 votes 0 votes Kapil commented Jul 12, 2016 reply Follow Share this is why we use static keyword , so that its value changes after each increment. it is not constant, everytime we enter the function... otherwise, we could have used only int , why static then.... 0 votes 0 votes shekhar chauhan commented Jul 12, 2016 reply Follow Share @DUW Get the difference Here . Output 0 votes 0 votes shekhar chauhan commented Jul 12, 2016 reply Follow Share @kapil Am i right ? 0 votes 0 votes Kapil commented Jul 12, 2016 reply Follow Share @shekhar sir, right one. well explained !! 0 votes 0 votes ankit commented Sep 15, 2016 reply Follow Share @Kapil and Shekhar sir , but here static variable declared in a function , So its scope and lifetime for that function only...So after 1st inc(); terminates , the memory location where 'x' is stored should also be destroyed and new x=0 will allcate for next inc() function... Please correct me if I'm wrong 0 votes 0 votes ManojK commented Sep 16, 2016 reply Follow Share @ankit " but here static variable declared in a function , So its scope and lifetime for that function only." The statement is not correct. Because the scope of static variable is entire file i.e. value of static variable is retained even after the function call ends.So this the difference between local variable and local static variable. See the difference: #include<stdio.h> int fun() { static int x = 0; x++; return x; } int main() { printf("%d ", fun()); printf("%d ", fun()); return 0; } So output:1,2 Now use normal local variable. #include<stdio.h> int fun() { int x = 0; x++; return x; } int main() { printf("%d ", fun()); printf("%d ", fun()); return 0; } So ouput :1,1 0 votes 0 votes ankit commented Sep 16, 2016 reply Follow Share ok sir now I understood... Thank u sir :) 1 votes 1 votes 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.