1,938 views

1 Answer

2 votes
2 votes

The static Storage Class

The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls.

The static modifier may also be applied to global variables. When this is done, it causes that variable's scope to be restricted to the file in which it is declared.

In C programming, when static is used on a global variable, it causes only one copy of that member to be shared by all the objects of its class.



#include <stdio.h>
 
/* function declaration */
void func(void);
 
static int count = 5; /* global variable */
 
main() {

   while(count--) {
      func();
   }
	
   return 0;
}

/* function definition */
void func( void ) {

   static int i = 5; /* local static variable */
   i++;

   printf("i is %d and count is %d\n", i, count);
}


When the above code is compiled and executed, it produces the following result −

i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0

Related questions

1 votes
1 votes
1 answer
1
arnav1827 asked Mar 21, 2023
1,232 views
One of the header fields in an IP datagram is the Time to Live (TTL) field. Which of the following statements best explains the need for this field ? It can be used to pr...
0 votes
0 votes
0 answers
2
Sanjay Sharma asked Oct 19, 2017
385 views
Data security in C programming languagecan be implemented using the data storageattribute—(A) Register(B) Static(C) Void(D) int
1 votes
1 votes
3 answers
3
Angkit asked Apr 23, 2017
14,551 views
Which sorting algorithm can be used to sort a random linked list with minimum time complexity ?A)mergesortB)quicksortC)radixsortD)insertionsortE)heapsort
0 votes
0 votes
0 answers
4