Variables
A variable is a named object with a type, a storage duration, and a scope. The type says how to interpret its bytes. The duration says how long those bytes exist. The scope says where the name is visible.
Detailed Explanation
A definition such as int count = 0; does four things. It reserves storage for an int. It attaches the name count to that storage. It sets the initial value to 0. It gives the name a scope — from the declaration to the end of the block for a local, or from the declaration to the end of the file for a file-scope object.
C has four storage durations. Automatic objects are locals without static; they appear when the block is entered and vanish when it ends. Static objects are file-scope variables and locals declared static; they exist for the whole program and are initialized once, before main. Thread-local objects, declared _Thread_local (or thread_local in C23), exist for one thread. Allocated objects come from malloc and live until free.
Scope and linkage are different knobs. Scope is about names: a local count hides a file-scope count for the length of the block. Linkage is about whether two declarations in different files name the same object. A file-scope static int count; has internal linkage and is private to the translation unit. A file-scope int count; has external linkage and is the same count the linker will look for in other files.
Initialization rules depend on duration. A static object with no initializer is zeroed: integers become 0, pointers become null, aggregates are filled recursively. An automatic object with no initializer holds an indeterminate value. Reading it is undefined behavior. Initialize at the point of declaration. C99 designated initializers let you name members: struct Point p = { .x = 1, .y = 2 };. C23 adds = {} as a zero initializer for any object.
A declaration can be a definition or a reference to a definition elsewhere. extern int count; says the object exists, somewhere, and has this type. int count; at file scope is a tentative definition; if no initializer appears in the translation unit, the compiler emits a zeroed definition. Two files both writing int count; used to be a common Unix idiom and is now a constraint violation under C23's removal of tentative-definition merging. Put the definition in one .c and extern in the header.
Code Examples
#include <stdio.h>
int file_count; /* file scope, external linkage, zeroed */
static int hidden; /* file scope, internal linkage: this .c only */
int main(void)
{
int local = 7; /* automatic, must be initialized before use */
static int visits = 0; /* one instance, survives across calls */
visits++;
{
int local = 99; /* hides the outer local for this block */
printf("%d\n", local); /* prints: 99 */
}
printf("%d %d %d\n", local, file_count, visits);
/* first call prints: 7 0 1 */
(void)hidden;
return 0;
}#include <stdio.h>
struct Point {
int x;
int y;
};
int main(void)
{
struct Point origin = { 0 }; /* x = 0, y = 0 */
struct Point p = { .y = 4, .x = 3 }; /* order does not matter */
int grid[3] = { [2] = 8 }; /* grid[0]=0, grid[1]=0, grid[2]=8 */
printf("%d %d %d\n", origin.x, p.y, grid[2]); /* prints: 0 4 8 */
return 0;
}Common Gotchas
An uninitialized automatic variable has an indeterminate value
even reading it to "see what it is" is undefined behavior. Write `int x = 0;` or assign before the first read.
A local `static` is shared across calls and across threads unless you add synchronization
it is a hidden global. Do not use it to stash per-request state in a library.
File-scope `int x;` in a header creates a definition in every file that includes it
put `extern int x;` in the header and `int x;` in one `.c`.
`const` does not change storage duration
`const int n = 3;` at block scope is still automatic. A compile-time constant integer is `enum` or `#define`, or in C23 `constexpr`.