Static in C

Static in C. Static in C Tutorial. Learn Static in C
Static in C

What is Static in C?

In C programming, the keyword “static” is used to specify the storage duration of a variable, function or data. The keyword “static” has different meanings depending on the context in which it is used.

The static keyword has several uses in C programming. Some common reasons for using static are:

  1. To retain the value of a variable between function calls.
  2. To hide implementation details from the outside world.
  3. To share data between functions within the same file.
  4. To reduce memory usage by sharing the same data across multiple instances of a function.

Types of Static Keyword in C:

1. Static Variable in C

There are two types of Static Variables:

  1. Static Local Variable
  2. Static Global Variable

1. Static Local Variable:

Static local variables are declared within a function block, similar to regular local variables. However, static local variables have a storage class of “static” and retain their value between function calls.

Example:

void function(){
  static int count = 0;
  count++;
  printf("%d\n",count);
}

In above example, the variable “count” is a static local variable that is initialized to zero. Each time the function is called, the value of “count” is incremented, and the new value is printed.

2. Static Global Variable:

Static global variables are declared outside of any function block and have a storage class of “static”. They are visible only within the file in which they are defined.

Example:

static int counter = 0;

In above example, the variable “counter” is a static global variable that is initialized to zero. It can be accessed by any function within the same file.

2. Static Function in C

A static function is a function that can only be called within the same file in which it is defined. It is not visible to other files that are linked together to create the final executable. This is useful for hiding implementation details that should not be exposed to the outside world.

Example:

static void helperFunction(){
  //some code
}

In above example, the function “helperFunction” is defined as static. It can only be called from within the same file in which it is defined.

3. Static Data in C

Static data is data that is allocated at compile time and persists throughout the lifetime of the program. It is not allocated on the stack or heap, but in a special area of memory that is reserved for static data.

Example:

static char message[] = "Hello, world!";

In this example, the character array “message” is defined as static. It is allocated at compile time and persists throughout the lifetime of the program.

Rules to follow while using Static keyword in C:

  1. Initialization: Static variables must be initialized before they are used. If you don’t provide an initial value, the variable will be initialized to zero.
  2. Scope: Static variables have a scope that is limited to the block in which they are defined. Static variables declared outside of any function block have file scope and can be accessed by any function within the same file.
  3. Lifetime: Static variables have a lifetime that is the same as the lifetime of the program. They are allocated when the program starts and retain their value until the program terminates.
  4. Concurrency: If multiple threads are accessing a static variable, you need to use synchronization mechanisms like mutexes or semaphores to ensure that the variable is accessed safely.
  5. Function pointers: When declaring a static function pointer, you should make sure that the function being pointed to has static storage duration as well. This ensures that the function pointer will still be valid even if the function it points to is no longer on the stack.
  6. Avoid overuse: While static variables and functions can be useful, overuse of the “static” keyword can lead to code that is hard to read, debug, and maintain. It is important to use static only when it is necessary for your program’s functionality.

Modifications that can be made while using Static in C:

When using the “static” keyword in C programming, you can modify its behavior by using some additional keywords.

1. Static const:

If you want to define a constant variable that has static storage duration, you can use the “static const” keyword. This ensures that the variable’s value is not modified during runtime.

Example:

static const int MAX_SIZE = 100;

2. Static inline:

If you want to define a function that is both static and inline, you can use the “static inline” keywords. This tells the compiler to inline the function’s code whenever it is called.

Example:

static inline int square(int x){
  return x*x;
}

3. Extern with Static:

If you want to use a static variable or function in another file, you can use the “extern” keyword along with the “static” keyword. This makes the variable or function visible outside of its file, but its scope is still limited to the file in which it is defined.

//file1.c
static int count = 0;
extern void printCount();

//file2.c
#include <stdio.h>
extern int count;
void printCount(){
  printf("Count = %d\n", count);
}

4. Static thread-local:

If you want to declare a variable that has static storage duration and is local to a specific thread, you can use the “static _Thread_local” keywords. This ensures that each thread has its own instance of the variable.

Example:

static _Thread_local int thread_count = 0;

More: