← Back to C Course | Chapter 14: Additional Topics | Lesson 4 of 9

C Static Functions & Variables

static keeps a variable's value between calls or hides a name inside one file, like a private notebook that stays in its room. It limits who can see something and how long it lives.
Syntax
c
return_type function_name() {
    static type variable = initial_value;   // keeps its value between calls
}

static type global_variable;               // visible only in this file
static return_type helper(void) { }        // private to this file

Static Local Variables

A static local variable is initialized only the first time its declaration is reached and then keeps its value between successive calls to the function, unlike an ordinary local variable which is recreated from scratch (and loses its previous value) every time the function runs.

Example: Static Local Variables

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void counter() {
	static int count = 0;
	count++;
	// Print formatted text to the screen
	printf("%d ", count);
}
// Program execution starts in main()
int main() {
	counter();
	counter();
	counter();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Static Global Variables

A static global variable is visible only within the source file where it's declared, meaning other .c files in the same project cannot access or accidentally modify it even if they declare a variable with the exact same name — the static keyword gives it file-level (internal) linkage instead of program-wide linkage.

Example: Static Global Variables

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
static int fileScoped = 42;
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", fileScoped);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Static Functions

Declaring a function static restricts its visibility to the file it's defined in, which is useful for internal helper functions that other files have no legitimate reason to call directly, keeping your project's externally usable API smaller and more intentional.

Example: Static Functions

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
static int helper(int n) {
	// Send n * 2 back to the caller
	return n * 2;
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", helper(5));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Scope Isolation with Static

Because static functions and static global variables are invisible outside their own file, two different .c files in the same project can safely declare a static function or variable with an identical name without the linker ever reporting a naming conflict between them.

Example: Scope Isolation with Static

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
static int value = 10;
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", value);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Memory Lifetime of Static Variables

Static variables live in the program's data segment rather than on the stack, meaning they are allocated exactly once when the program starts and continue occupying that memory for the program's entire runtime, regardless of how many times the function containing them is called.

Example: Memory Lifetime of Static Variables

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void increment() {
	static int total = 0;
	total++;
	// Print formatted text to the screen
	printf("%d ", total);
}
// Program execution starts in main()
int main() {
	increment();
	increment();
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting a static local variable to be reset each call, when it keeps its value.
  2. Trying to call a static function from another file, which fails because it is private to its file.
  3. Confusing static global (file-private) with a normal global that other files can use with extern.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.