Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Basics of C Programming
  5. Storage Classes
  6. Static Storage Class

Static Storage Class

Static Storage Class

  • Storage class specifier static specifies that the declared object will have static (global) life time.
  • Objects that are declared with static storage class are stored in main memory
  • It can be used to declare the objects in local scope as well as in global scope
  • The variables declared with static storage class are implicitly initialized, the value will be initialized to 0 for int type, 0.0 for float type and ‘\0’ for char type
  • If a static variable is present inside the local scope, the associated object is initialized only once. The object will not be reinitialized even if the program control re-enters the block in which the variable is declared. Thus, the value of static variable persists between the function calls.
  • Static storage class cannot be used in the parameter declaration either in the function declaration or in the function call

Example

#include <stdio.h>
void display();
int main()
{
    display();
    display();
    return 0;
}
void display()
{
    static int c = 1;
    c += 5;
    printf("%d  ",c);
}

Output

C:\TDM-GCC-64>gcc static.c -o static
C:\TDM-GCC-64>static
6  11

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments