Back to C Programming
2026-07-125 min read

C - Static Keyword

Learn C - Static Keyword step by step with clear examples and exercises.

Title: Understanding Static Keyword in C - A full guide for C Programmers

Why This Matters

In C programming, the static keyword is a powerful tool that offers unique features to variables and functions. It's crucial for C programmers to understand its usage as it helps manage memory effectively and avoid common pitfalls during program development. The static keyword plays an essential role in interviews and real-world coding scenarios, where efficient memory management can make a significant difference.

Prerequisites

To fully grasp the concept of the static keyword in C, you should have a solid understanding of:

  1. Basic C syntax, including variables, functions, and data types
  2. Scope rules in C programming
  3. Memory management concepts such as stack and heap memory
  4. The difference between local and global variables

Core Concept

Definition

In C programming, the static keyword can be used to declare variables with local-static storage class or static functions. These static entities have a different behavior compared to their non-static counterparts.

Static Variables

A static variable is a local variable that retains its value between function calls. By default, local variables are created and destroyed each time a function is called. However, when you declare a variable as static, it will maintain its value even after the function returns.

Here's an example demonstrating the difference between a regular local variable and a static one:

#include <stdio.h>

void increment() {
int counter = 0; // Regular local variable
static int staticCounter = 0; // Static local variable

counter++;
printf("Local counter: %d\n", counter);

staticCounter++;
printf("Static counter: %d\n", staticCounter);
}

int main() {
increment();
increment();
increment();

return 0;
}

In this example, the output will be:

Local counter: 1
Static counter: 1
Local counter: 2
Static counter: 2
Local counter: 3
Static counter: 3

As you can see, the local variable counter is reset each time the function increment() is called, while the static variable staticCounter maintains its value between calls.

Static Functions

A static function in C cannot be called from outside its defining file. This feature allows you to create helper functions that are exclusively used within a particular source file without exposing them to other files or users of the library.

Here's an example demonstrating the usage of a static function:

// my_functions.c
#include <stdio.h>

void myFunction() {
printf("Hello from myFunction!\n");
}

static void staticMyFunction() {
printf("Hello from staticMyFunction!\n");
}

int main() {
myFunction(); // Calls the non-static function

return 0;
}

// my_main.c
#include "my_functions.h"

int main() {
staticMyFunction(); // Trying to call the static function will result in a compile error

return 0;
}

In this example, you'll get a compile error when trying to call staticMyFunction() from my_main.c. The static function is only accessible within its defining file (my_functions.c).

Worked Example

Let's create a simple program that uses a static variable to maintain the count of how many times a function has been called:

#include <stdio.h>

void counter() {
static int calls = 0;

printf("Function call number %d\n", ++calls);
}

int main() {
for (int i = 0; i < 5; i++) {
counter();
}

return 0;
}

In this example, the counter() function is called five times using a loop. The static variable calls maintains its value between calls, and the output will be:

Function call number 1
Function call number 2
Function call number 3
Function call number 4
Function call number 5

Common Mistakes

  1. Forgetting to initialize static variables: Static variables retain their value between function calls, but they are not automatically initialized like global variables. Make sure you initialize them properly to avoid unexpected behavior.
  1. Misusing static functions: Remember that static functions can only be called from within their defining file. Attempting to call a static function from another file will result in a compile error.
  1. Incorrectly using static variables with global scope: If you declare a variable as both static and extern, it will have internal linkage, which means it can only be accessed within the file where it is defined. Be careful when deciding whether to use global or static variables based on your program's requirements.

Practice Questions

  1. Write a C program that uses a static variable to count the number of times a function is called and prints the average number of calls after calling the function 100 times.
  2. Modify the previous example to create a static function that calculates the factorial of a given number and returns the result. Test your implementation by calling the function from main() with different input values.
  3. Write a C program that uses static functions to implement a simple encryption algorithm for a custom message. Create two static functions: one for encoding and another for decoding the message.

FAQ

What happens if I declare a global variable as static?

A static global variable has internal linkage, which means it can only be accessed within its defining file. This is similar to declaring a static local variable.

Can I call a static function from another source file?

No, static functions can only be called from within their defining file.

What's the difference between static and const in C?

static is used to declare variables or functions with specific storage classes, while const is used to declare constants that cannot be modified after initialization.

Can I use static with arrays in C?

Yes, you can declare an array as static, but remember that it will only retain its value within the function where it's defined.

What happens when a program terminates? Do static variables persist?

When a program terminates, all automatic and dynamic memory is freed, including stack memory used for local variables. However, static variables are stored in data segment memory, which persists until the program is reloaded or the system is restarted.