CSS Computer Science (Optional) — Paper-I, Section-A, Topic II subtopic. Appeared as a past paper question (2017).
Local Variables
- Declared inside a function/block
- Scope limited to that function/block only — inaccessible outside it
- Lifetime: created when the function is called, destroyed when it returns
- Stored typically on the stack
Global Variables
- Declared outside all functions, usually at the top of the file
- Scope extends across the entire program (all functions can access/modify it)
- Lifetime: exists for the entire duration of the program's execution
- Stored typically in a fixed data segment, not the stack
Shared Variables
- Variables accessible by multiple parts of a program running concurrently (e.g. multiple threads/processes)
- Common in multi-threaded/parallel programming — require synchronization (locks, mutexes) to avoid race conditions
- Not a standard single-language keyword category everywhere, but a conceptual term for variables intentionally exposed across execution units
Storage Classes (C/C++)
| Storage Class |
Scope |
Lifetime |
Default Value |
| auto |
Local (block) |
Function call duration |
Garbage |
| static |
Local or global |
Entire program run |
Zero |
| extern |
Global (declared elsewhere) |
Entire program run |
Zero |
| register |
Local (block) |
Function call duration |
Garbage |
- A static local variable is declared inside a function but keeps its value between calls (unlike a plain local/
auto variable, which resets each call) — a classic exam trap
- extern declares a variable that is defined in another file/translation unit, allowing it to be shared across multiple source files
- register is a hint to the compiler to store the variable in a CPU register for faster access (modern compilers mostly ignore this and optimize automatically)