Skip to main content

Syntax of static

Short answer

static data_type variable_name = value; makes a local variable persist across function calls, initialized only once and defaulting to zero. It retains its value for the program's lifetime while remaining locally scoped.

Write the syntax used in C programming language for static.

static is a storage class. A static local variable is created only once, keeps its value between function calls and exists for the whole run of the program, although it is visible only inside its block. It is initialised only once, and to 0 by default. (A static global variable or function is visible only within its own source file.)

Syntax: static data_type variable_name = value;

#include <stdio.h>
void counter(void)
{
    static int count = 0;    /* initialised once, keeps value between calls */
    int normal = 0;
    count++;
    normal++;
    printf("static = %d, auto = %d\n", count, normal);
}
int main(void)
{
    counter();
    counter();
    counter();
    return 0;
}

Output:

static = 1, auto = 1
static = 2, auto = 1
static = 3, auto = 1

The static variable keeps counting (1, 2, 3), whereas the normal local variable starts again at 0 in every call.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 6 questions →