Scope Rules and Storage Class Specifiers
STORAGE CLASS MODIFIERS
the four set of keyword namely static, extern , auto , register are called storage class modifiers which modify the storage location of a variable and hence affect its scope and lifetime.
global variable - a variable which is declared outside all blocks/functions (even main()) is called a global variable Such a variable is available across all files and function of that program
local variable - a variable defined inside some containing block is called a local variable
-We may use any of the above keywords with both the global and local variables.
-By default any variable (if no storage class specifier is specified) is declared as auto.
In the following discussion we use global / local to refer to global / local variable defined with auto keyword or without any class specifier
SCOPE
The scope of a variable basically refers the parts of the program where the variable is accessible or more technically 'visible'.
global (or more specifically a global auto variable)-across all files and function of the program/project
static, local, register - scope same as for auto i.e limited to the declaring block
in case of a global static variable the scope is restricted in the above mentined manner i.e. to the declaring block i.e the file which declares the variable
extern - the use of extern keyword restricts definiton of the variable to a declaration. Hence it does not actually modify the scope of that variable. It simply tells the compiler that somewhere in the current program and in the current scope there is a variable (say a) and it should use that variable .
The scope and lifetime of any variable is contolled by its definition.
Thus if we try to define a variable after its extern declaration within the same block it results in a linker error. Again a variable declared as extern in any program should BE DEFINED at some or the other place in the program .
LIFETIME
The lifetime or longevity refers to the duration for which the variable remains in the memory.
global ,static - program run
register, local - until end of function /block
STORAGE ALLOCATION
The memory available to any program has two parts
Data Segment (Data + BSS + Heap)
Stack
The Data area contains global variables used by the program that are not initialized to zero. For instance the string defined by char s[] = "hello world"; in C would exist in the data part.
The BSS segment starts at the end of the data segment and contains all global variables that are initialized to zero. For instance a variable declared static int i; would be contained in the BSS segment.
The heap area begins at the end of the BSS segment segment and grows to larger addresses from there. The Heap area is managed by malloc, realloc, and free, which use the brk and sbrk system calls to adjust its size. The Heap area is shared by all shared libraries and dynamic load modules in a process.
The STACK is a LIFO structure, typically located in the higher parts of memory. It usually "grows down" with every register, immediate value or stack frame being added to it. A stack frame consists at minimum of a return address.
global -data segment ==> data/BSS
static -data segment==> BSS
register - CPU registers
local/auto (not global) - stack




Recent comments
11 weeks 10 hours ago
1 year 8 weeks ago
1 year 11 weeks ago
1 year 11 weeks ago
1 year 12 weeks ago
1 year 13 weeks ago
1 year 14 weeks ago
1 year 14 weeks ago
1 year 18 weeks ago
1 year 18 weeks ago