0 votes 0 votes Why 'count' variable value doesn't set to 0 on every call to 'incr' function? Programming in C algorithms programming-in-c + – shikharV asked Nov 18, 2015 • recategorized Jul 6, 2022 by Lakshman Bhaiya shikharV 2.0k views answer comment Share Follow See all 4 Comments See all 4 4 Comments reply Lakshman Bhaiya commented Jul 31, 2017 reply Follow Share All(Local or Global) Static variable life time throughout the program but Local static variable scope in the define function and Global variable throughout the program. (*******) All static variable initialize with zero by default (*******) All Global variable initialize with zero by default 0 votes 0 votes khyatinetam commented Mar 14, 2018 reply Follow Share Here the count is static variable and its value is zero and as we know that scope of static variable is throughout the program. when i=0 function incr(0) is called count =count+i => 0+0 =0 when i=1 function incr(1) is called count =count+i => 0+1 =1 when i=2 function incr(2) is called count =count+i => 1+2 =3 when i=3 function incr(3) is called count =count+i => 3+3 =6 when i=4 function incr(4) is called count =count+i => 6+4 =10 when i=5 function incr(5) is called count =count+i => 10+5 =15 when i=6 loop terminates ANSWER IS COUNT = 15 0 votes 0 votes Rajat Negi commented Jul 12, 2018 reply Follow Share Isnt the count value set to 0 on each function call. If it is set to zero on each call. The value of j should b 5 at the end of program. 0 votes 0 votes Lakshman Bhaiya commented Jul 12, 2018 reply Follow Share No, the count is a static variable. when you write static int x = 0; this line executes once and always x takes the latest value. ---->Static variable initialized only once. see https://www.geeksforgeeks.org/static-variables-in-c/ 1 votes 1 votes Please log in or register to add a comment.
Best answer 5 votes 5 votes here static int count= 0 means memory create for count for compile time only ..ans will be 15 only at cal(0)= 0 at cal(1)= 1 at cal(2)=3 at cal(3)= 6 at cal(4)= 10 at cal(5)=15 What ur talking about constant int count= 0. Prashant. answered Nov 18, 2015 • selected Nov 18, 2015 by shikharV Prashant. comment Share Follow See all 0 reply Please log in or register to add a comment.
1 votes 1 votes Count is a static variable which allocates its memory in data segment unlike the usual stack . Therefore when a function is called again the value still remains stored and it refers to the old location in data segment every time. Had count been a local variable (int count =0 )it would be stored in stack and then every time function is called count would be allocated different memory locations in stack since the activation records are different for each call of the function. Then what you asked would have been true and j value would be 5. Riya Roy(Arayana) answered Nov 18, 2015 Riya Roy(Arayana) comment Share Follow See all 4 Comments See all 4 4 Comments reply shikharV commented Nov 18, 2015 reply Follow Share Is the statement containing the declaration of static variable will be skipped after the first function call? 0 votes 0 votes Prashant. commented Nov 18, 2015 reply Follow Share i will be like that when static come next time then it will tell memory created already at compile time so dnt creat now. 1 votes 1 votes Riya Roy(Arayana) commented Nov 18, 2015 reply Follow Share @sv_jan5 yes you can think it as if it is skipped but the reality is it is pointing to same memory location every time. 0 votes 0 votes shikharV commented Nov 18, 2015 reply Follow Share ok now its clear. 0 votes 0 votes Please log in or register to add a comment.
0 votes 0 votes 15 Because static variables initialize only on time. Nitesh Choudhary answered Apr 19, 2017 Nitesh Choudhary comment Share Follow See all 0 reply Please log in or register to add a comment.