乐闻世界logo
搜索文章和话题

What is the scope and lifetime of the static keyword in C language?

2月18日 17:18

What is the scope and lifetime of the static keyword in C language?

Three Uses of static Keyword:

  1. Static Local Variables

    c
    void counter() { static int count = 0; // Initialized only once count++; printf("Count: %d\n", count); } int main() { counter(); // Count: 1 counter(); // Count: 2 counter(); // Count: 3 }
  2. Static Global Variables

    c
    static int global_var = 100; // File scope void function() { global_var++; } // Other files cannot access global_var
  3. Static Functions

    c
    static void helper_function() { printf("This is a static function\n"); } // Can only be called within current file

Scope and Lifetime:

  1. Static Local Variables

    c
    void example() { // Scope: Within function // Lifetime: Entire program execution static int value = 10; // Retains previous value on each call value++; }
  2. Static Global Variables

    c
    // file1.c static int file_global = 50; // file2.c extern int file_global; // Link error
  3. Static Functions

    c
    // file1.c static int internal_calculation(int x) { return x * 2; } // file2.c int internal_calculation(int x); // Link error

Practical Application Scenarios:

  1. Singleton Pattern

    c
    int* get_instance() { static int *instance = NULL; if (instance == NULL) { instance = malloc(sizeof(int)); *instance = 0; } return instance; }
  2. Caching Mechanism

    c
    int expensive_calculation(int n) { static int cache[100] = {0}; if (n < 100 && cache[n] != 0) { return cache[n]; } int result = /* complex calculation */; if (n < 100) { cache[n] = result; } return result; }
  3. Counter

    c
    int get_unique_id() { static int next_id = 0; return next_id++; }
  4. State Maintenance

    c
    void state_machine() { static enum { INIT, RUNNING, DONE } state = INIT; switch (state) { case INIT: printf("Initializing...\n"); state = RUNNING; break; case RUNNING: printf("Running...\n"); state = DONE; break; case DONE: printf("Done!\n"); break; } }

Difference from const:

  1. static vs const
    c
    // static: Controls scope and lifetime static int value = 10; // const: Controls modifiability const int value = 10; // Can be combined static const int CONSTANT = 100;

Thread Safety Considerations:

  1. Non-Thread-Safe Static Variables

    c
    int* get_instance_unsafe() { static int *instance = NULL; // Not thread-safe if (instance == NULL) { instance = malloc(sizeof(int)); } return instance; }
  2. Thread-Safe Static Variables

    c
    #include <pthread.h> int* get_instance_safe() { static int *instance = NULL; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock(&mutex); if (instance == NULL) { instance = malloc(sizeof(int)); } pthread_mutex_unlock(&mutex); return instance; }

Best Practices:

  1. Information Hiding

    c
    // module.c static int internal_state = 0; static void internal_helper() { internal_state++; } void public_interface() { internal_helper(); }
  2. Avoid Global Variable Pollution

    c
    // Use static to limit scope static int module_config = 0; void set_config(int value) { module_config = value; }
  3. Initialization Order

    c
    void function() { static int initialized = 0; static int cache[100]; if (!initialized) { for (int i = 0; i < 100; i++) { cache[i] = i * i; } initialized = 1; } return cache[10]; }
标签:C语言