recategorized by
2,017 views
0 votes
0 votes

Why 'count' variable value doesn't set to 0 on every call to 'incr' function?

recategorized by

3 Answers

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. 

selected by
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.

Related questions

1 votes
1 votes
1 answer
2
iarnav asked Apr 18, 2017
692 views
Please kindly tell the full code with the use of sizeof() operator. Thanks!
0 votes
0 votes
3 answers
3
ykrishnay asked May 30, 2022
1,094 views
int main(){extern int a = 20;printf("%d",a); }//why this gives error but followig code is not extern int a =20;int main(){printf("%d",a); } //why this happens that first ...