On this page
  1. static
  2. Availability
  3. Syntax & Overview
  4. Notes & Pitfalls
  5. Example

static

Prev: const | Up: builtins | Next: extern
Rel: Control Flow & Functions, extern
Ref: cppreference — static
Tags: #public #prog #c #builtins #tier/core


Availability

Built-in (Storage-Class Specifier) — requires no #include header.

Syntax & Overview

static T x;  |  static T fn(void);

Gives file-scope functions/variables internal linkage (private to the .c file), or gives block-scope locals static storage duration.

Notes & Pitfalls

Example

#include <assert.h>

static int next_id(void) {
    static int id = 0;
    return ++id;
}
int main(void) { assert(next_id() == 1 && next_id() == 2); }

Linked from

Related notes