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.
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.
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.
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'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'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.