Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The keyword <code>static</code> is used in several different ways in C. (There's a joke that any new version of the ISO C standard is required to invent a new meaning for <code>static</code>.)</p> <p>Quick summary: The <code>static</code> keyword on a block scope definition gives the entity static storage duration; at file scope, where definitions already have static storage duration, it instead gives them internal linkage.</p> <p>An <em>object</em> may have any of several <em>storage durations</em>. An object with "automatic" storage duration exists at run time only during the execution of the enclosing block. An object with "static" storage duration exists during the entire execution of the program. (There are also "allocated" and, new in C11, "temporary" storage durations, which I won't get into.)</p> <p>Any definition of an identifier also has a "linkage", which can be external, internal, or none. Linkage controls whether an identifier is usable across translation units (basically source files, but <code>#include</code>d files are not separate translation units). Linkage, as the name implies, has to do with the linker. You can use the same identifier with internal linkage in two different source files, and it will refer to two different entities. But if an identifier has external linkage, it should be <em>defined</em> only once, and can be <em>declared</em> (typically with <code>extern</code>) in multiple source files; all those declarations will refer to the same entity.</p> <p>If you define an object (variable) at block scope, (i.e., inside a function body), it has automatic storage duration (and no linkage) by default. Adding the keyword <code>static</code> gives it static storage duration, so it exists and retains its value across calls to the function. (It doesn't affect the identifier's visibility).</p> <p>If you define an object at file scope (i.e., outside any function body), it has static storage duration and external linkage by default. Adding the keyword <code>static</code> to the definition doesn't affect its storage duration, but it changes its linkage from external to internal, hiding the name from other translation units. Functions don't have storage duration (their code exists as long as the program is running), but <code>static</code> affects their linkage the same way, changing it from external to internal.</p> <p>(C99 added another meaning for <code>static</code>, for function parameters of array type, which has nothing to do with the other uses.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload