Variables in C Language

Scope(visibility)Lifetime(duration)Automatic
deallocated after block
Register
stored in CPU register
Static
persists between calls
Program
exists entire runtime
Block
within {} only
no modifier
Block-local variable
const
Block-local constant
volatile
Can change externally
restrict
No pointer aliasing
no modifier
Block register
const
Constant register
restrict
No pointer aliasing
N/AN/A
Local
function scope
no modifier
Basic local variable
const
Immutable local value
volatile
Can change unexpectedly
restrict
No pointer aliasing
no modifier
Basic register variable
const
Read-only register
restrict
No pointer aliasing
no modifier
Retains value between calls
const
Immutable static value
volatile
Can change externally
restrict
No pointer aliasing
N/A
File-scope
current file only
N/AN/A
no modifier
File-wide access
const
File constant
volatile
Can change externally
restrict
No pointer aliasing
N/A
Extern
other files
N/AN/AN/A
no modifier
External linkage
const
External constant
volatile
External changes
restrict
No pointer aliasing
Global
other files
N/AN/AN/A
no modifier
External linkage
const
External constant
volatile
External changes
restrict
No pointer aliasing





Variable Scope in C

Variable scope in C determines where in the program a variable can be accessed. C provides several levels of scope: local (function) scope, block scope, file scope, and program scope. Understanding scope is crucial for writing maintainable and bug-free code, as it helps prevent naming conflicts and ensures proper variable lifetime management.



Block variable in C is a variable declared inside a block (delimited by curly braces) and visible only within that block.
It's scope starts at the point of declaration and continues until the closing brace of the block containing the declaration.
The variable cannot be accessed from outside the containing block, regardless of it's storage duration or other attributes.
Block-Scope Variables#include <stdio.h>void main() { // Regular block variable: // - Lives only in this block // - Can be modified // - Stored in memory { int count = 0; count++; // Can change value ... ... ... } // Value lost here // Constant block variable: // - Lives only in this block // - Cannot be modified after initialization // - Compiler error if try to change { const int MAX = 100; MAX = 200; // Error! Cannot modify ... ... ... } // Value lost here // Register block variable: // - Lives only in this block // - May be stored in CPU register (faster access) // - Cannot get its address with & { register int idx = 0; &idx; // Error! Cannot get address ... ... ... } // Value lost here // Volatile block variable: // - Lives only in this block // - Tells compiler value can change externally // - Must read from memory each time { volatile int status = 1; while(status) { // Will check memory each time ... ... ... } } // Value lost here}Block scoped variables exist only within its block and are deallocated after


Local variable (also known as a function variable) in C is a variable declared inside a function and visible throughout that function.
Its scope begins at the point of declaration and extends to the end of the function, including any blocks nested within the function where it is not shadowed by a block variable of the same name.
The function variable is not accessible from outside its containing function, including from other functions in the same file.


File variable (also known as file-scope variable) in C is a variable declared outside any function in a source file and visible from it's point of declaration to the end of that file.
The scope of file variable extends through all functions in the file, but it is not accessible from other source files unless explicitly declared as extern.
File scope represents a broader visibility than function scope but remains contained within its translation unit.


Extern variable in C has program-wide scope, meaning it can be accessed across multiple source files.

Its scope extends throughout the entire program, making it visible to any source file that declares it with the extern keyword, though the actual variable must be defined (storage allocated) in exactly one source file.
This represents the almost broadest possible scope in C, allowing shared access to the variable across the program's translation units.


Global variable (also known as program variable) in C has program-wide scope, making it accessible throughout the entire program.
It's scope begins at the point of declaration and continues through all functions and files in the program.
Like extern variables, it represents the broadest scope level in C, but unlike extern variables, it is both defined and declared in the same place, making it directly accessible without additional extern declarations.
C Variable Scope HierarchyBlockLocalFileExternGlobal

Variable Scopes in CGlobalExternFileLocalBlock
↑ TopNext →


Variable Lifetime in C

Variable lifetime in C determines how long a variable maintains its value and exists in memory. Different storage classes provide different lifetimes, from short-lived automatic variables to program-wide static storage.




Time → Program Runtime Function Call 1 Function Call 2 Block 1 Block 2 Block 3 Block 4 Program Variables Static Variables Register/Auto Variables Variable Types: Program (Entire runtime) Static (Between calls) Register/Auto (Block)



Automatic Variables: A variable with automatic lifetime exists only within its block of declaration and is automatically deallocated when the block execution ends. Each time the block is entered, the variable is recreated, and its value must be reinitialized. It&apos;s the default lifetime for local variables and provides efficient memory management for temporary data.



Register Variables: A register variable has automatic lifetime but suggests storage in CPU registers rather than main memory for faster access. Like automatic variables, they exist only during block execution and are deallocated upon exit. However, their physical storage location (whether actually in a register or not) is ultimately determined by the compiler&apos;s optimization decisions.



Static Variables: A static variable persists between function calls, maintaining its value throughout program execution once initialized. Unlike automatic variables, it is initialized only once when program execution begins and retains its memory location even when out of scope. This provides a way to maintain state information between function invocations.



Program Variables: A program variable exists for the entire runtime of the program, from start to finish. It is allocated when the program begins execution and deallocated only when the program terminates. These variables, typically global or extern, provide program-wide data storage and maintain their values throughout the entire program execution.

↑ TopNext →