Skip to content
doubtly

2 answers

A static variable keeps its value for the whole run of the program; a static function (or static global variable) can be used only inside the source file in which it is defined.

Static local variable: declared inside a function with static. Its scope is still that function, but it is created and initialised only once and is not destroyed when the function returns, so it remembers its value between calls. If not initialised, it starts at 0.

#include <stdio.h>
void counter() {
    static int c = 0;   /* initialised only once */
    c++;
    printf("%d ", c);
}
int main() {
    counter(); counter(); counter();   /* prints 1 2 3 */
    return 0;
}

Without static, c would be re-created as 0 on every call and the output would be 1 1 1.

Static global variable: declared outside all functions with static; it is visible only in that file and cannot be accessed from other files with extern.

Static function: a function declared as static int helper(void) { ... } has internal linkage, so it can be called only by functions in the same source file. This hides helper functions and avoids name clashes in large programs.

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.