When you create a variable in C, you must understand two important characteristics of it:
🔍 Scope — “Where” a variable can be accessed
Think of scope as the “visibility” of a variable — it defines in which parts of your code the variable is known and can be used.
⏳ Lifetime — “How long” the variable exists in memory
Think of lifetime as the “duration” for which the variable stays in memory (RAM) during the program's execution.
🔍 Scope of Variables in C
Scope is divided into 4 major types:
1️⃣ Local Scope (Block Scope)
A variable declared inside a function or a block (
{}) is called a local variable.It can only be accessed inside that function/block.
It is created when the block is entered and destroyed when the block ends.
📌 Example:
void myFunction() {
int x = 10; // x has local scope
printf("%d", x);
}
// printf("%d", x); ❌ This will give an error: x is not accessible here2️⃣ Global Scope
A variable declared outside all functions, usually at the top of the file.
It can be accessed by all functions in that file (or across files with
extern).It is created when the program starts and destroyed when the program ends.
📌 Example:
int x = 5; // Global variable
void show() {
printf("%d", x); // Accessible
}
int main() {
printf("%d", x); // Also accessible
}3️⃣ Function Parameters Scope
Parameters in functions act like local variables.
They are created when function is called and destroyed when function ends.
📌 Example:
void show(int num) {
printf("%d", num); // num has scope only within show()
}4️⃣ File Scope (with static)
If you declare a global variable with
static, it will only be visible within the current file — not accessible from other files.
📌 Example:
static int counter = 0; // File scope, accessible only within this file⏳ Lifetime of Variables in C
Let’s now understand how long variables stay in memory, based on their storage class.
🧱 Automatic (Local) Variables
Declared inside a function without
static.Lifetime: From function entry to function exit.
Stored in stack memory.
Declared with
auto(default, so often skipped).
void show() {
int x = 10; // automatic variable
}🗃️ Static Variables
Declared with
statickeyword (can be local or global).Lifetime: Entire program run.
Retains value between function calls.
Stored in data segment (not stack).
📌 Example:
void counterFunc() {
static int count = 0; // Initialized only once
count++;
printf("%d\\n", count);
}
int main() {
counterFunc(); // 1
counterFunc(); // 2
counterFunc(); // 3
}🌍 Global Variables
Declared outside any function.
Lifetime: Entire program.
Accessible from all functions in the same file.
int x = 100;
void test() {
printf("%d", x); // Still alive here
}🌐 Extern Variables
Declared in another file, accessed using
extern.Lifetime is also for the entire program.
// file1.c
int x = 100;
// file2.c
extern int x;<div class="infographic-cscopelife">
<style>
.infographic-cscopelife {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--code-bg: #1e1e2e;
--code-text: #d4d4e0;
--syn-keyword: #c586c0;
--syn-func: #dcdcaa;
--syn-string: #ce9178;
--syn-comment: #6a9955;
}
.infographic-cscopelife[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--code-bg: #0d0d14;
--code-text: #d4d4e0;
}
.infographic-cscopelife {
box-sizing: border-box;
max-width: 780px;
margin: 0 auto;
background: var(--paper);
color: var(--ink);
font-family: 'Inter', -apple-system, sans-serif;
padding: 0 clamp(1rem, 4vw, 2.25rem) 1.25rem;
}
.infographic-cscopelife *, .infographic-cscopelife *::before, .infographic-cscopelife *::after {
box-sizing: border-box;
}
.infographic-cscopelife h1 {
font-family: 'Fraunces', Georgia, serif;
font-weight: 600;
font-size: clamp(1.4rem, 3.5vw, 1.9rem);
margin: 1.25rem 0 0.9rem;
}
.infographic-cscopelife .group-label {
font-size: 0.72rem;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.04em;
color: var(--ink-soft);
margin: 0 0 0.4rem;
}
.infographic-cscopelife .chip-row { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-bottom: 0.9rem; }
.infographic-cscopelife .chip {
all: unset;
cursor: pointer;
font-size: 0.8rem;
font-weight: 600;
padding: 0.4rem 0.8rem;
border-radius: 20px;
border: 1px solid var(--line);
color: var(--ink-soft);
transition: background 0.2s, color 0.2s, border-color 0.2s;
}
.infographic-cscopelife .chip:hover { border-color: var(--accent); }
.infographic-cscopelife .chip.is-active { background: var(--accent); border-color: var(--accent); color: #fff; }
.infographic-cscopelife .code-panel {
background: var(--code-bg);
color: var(--code-text);
border-radius: 8px;
padding: 0.75rem 0.95rem;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.8rem;
line-height: 1.65;
margin: 1.1rem 0;
overflow-x: auto;
-webkit-overflow-scrolling: touch;
max-width: 100%;
}
.infographic-cscopelife .code-line {
display: block;
white-space: pre;
border-radius: 4px;
padding: 0.05rem 0.4rem;
margin: 0 -0.4rem;
transition: background 0.3s, box-shadow 0.3s;
}
.infographic-cscopelife .code-line.is-highlight {
background: rgba(91, 141, 239, 0.22);
box-shadow: inset 3px 0 0 var(--accent);
}
.infographic-cscopelife .syn-keyword { color: var(--syn-keyword); }
.infographic-cscopelife .syn-func { color: var(--syn-func); }
.infographic-cscopelife .syn-string { color: var(--syn-string); }
.infographic-cscopelife .syn-comment { color: var(--syn-comment); }
@media (prefers-reduced-motion: reduce) {
.infographic-cscopelife * { transition: none !important; }
}
</style>
<h1>🔍 Scope & Lifetime in C</h1>
<div class="group-label">Scope</div>
<div class="chip-row" id="csl-scope-chips">
<button class="chip" data-topic="local">Local</button>
<button class="chip" data-topic="global">Global</button>
<button class="chip" data-topic="param">Function Parameter</button>
<button class="chip" data-topic="filescope">File (static)</button>
</div>
<div class="group-label">Lifetime</div>
<div class="chip-row" id="csl-life-chips">
<button class="chip" data-topic="automatic">Automatic</button>
<button class="chip" data-topic="staticlife">Static</button>
<button class="chip" data-topic="globallife">Global</button>
<button class="chip" data-topic="extern">Extern</button>
</div>
<div class="code-panel" id="csl-code">
<span class="code-line" data-line="L1"><span class="syn-keyword">static</span> <span class="syn-keyword">int</span> counter = 0; <span class="syn-comment">// file.c</span></span>
<span class="code-line"> </span>
<span class="code-line" data-line="L3"><span class="syn-keyword">int</span> x = 100; <span class="syn-comment">// file.c</span></span>
<span class="code-line"> </span>
<span class="code-line" data-line="L5"><span class="syn-keyword">void</span> <span class="syn-func">show</span>(<span class="syn-keyword">int</span> num) {</span>
<span class="code-line" data-line="L6"> <span class="syn-keyword">int</span> local = num * 2;</span>
<span class="code-line" data-line="L7"> <span class="syn-func">printf</span>(<span class="syn-string">"%d"</span>, local);</span>
<span class="code-line" data-line="L8">}</span>
<span class="code-line"> </span>
<span class="code-line" data-line="L10"><span class="syn-keyword">void</span> <span class="syn-func">counterFunc</span>() {</span>
<span class="code-line" data-line="L11"> <span class="syn-keyword">static</span> <span class="syn-keyword">int</span> count = 0;</span>
<span class="code-line" data-line="L12"> count++;</span>
<span class="code-line" data-line="L13">}</span>
<span class="code-line"> </span>
<span class="code-line" data-line="L15"><span class="syn-keyword">extern</span> <span class="syn-keyword">int</span> total; <span class="syn-comment">// file2.c</span></span>
<span class="code-line"> </span>
<span class="code-line" data-line="L17"><span class="syn-keyword">int</span> <span class="syn-func">main</span>() {</span>
<span class="code-line" data-line="L18"> <span class="syn-func">show</span>(x);</span>
<span class="code-line" data-line="L19"> <span class="syn-func">counterFunc</span>();</span>
<span class="code-line" data-line="L20"> <span class="syn-func">printf</span>(<span class="syn-string">"%d"</span>, total);</span>
<span class="code-line" data-line="L21"> <span class="syn-keyword">return</span> 0;</span>
<span class="code-line" data-line="L22">}</span>
</div>
</div>
<script>
(function () {
const wrapper = document.currentScript.closest('.infographic-cscopelife')
|| document.querySelector('.infographic-cscopelife');
const bodyAttr = document.body.getAttribute('data-darkmode');
let dark;
if (bodyAttr === 'true') { dark = true; }
else if (bodyAttr === 'false') { dark = false; }
else {
const match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) wrapper.setAttribute('data-theme', 'dark');
const allChips = wrapper.querySelectorAll('.chip');
const codeLines = wrapper.querySelectorAll('#csl-code .code-line[data-line]');
const allLineIds = Array.from(codeLines).map(l => l.dataset.line);
const TOPIC_LINES = {
local: ['L5', 'L6', 'L7', 'L8'],
global: allLineIds,
param: ['L5', 'L6', 'L7', 'L8'],
filescope: allLineIds,
automatic: ['L5', 'L6', 'L7', 'L8'],
staticlife: ['L10', 'L11', 'L12', 'L13'],
globallife: allLineIds,
extern: ['L15', 'L20']
};
function selectTopic(topic, activeChip) {
allChips.forEach(c => c.classList.remove('is-active'));
activeChip.classList.add('is-active');
const ids = TOPIC_LINES[topic] || [];
codeLines.forEach(l => l.classList.toggle('is-highlight', ids.indexOf(l.dataset.line) !== -1));
}
allChips.forEach(chip => {
chip.addEventListener('click', () => selectTopic(chip.dataset.topic, chip));
});
// default: highlight nothing until a topic is picked
})();
</script>🧪 Summary Table
Type | Scope | Lifetime | Keyword | Stored In |
|---|---|---|---|---|
Local (auto) | Inside block | Till block ends | auto (default) | Stack |
Global | Entire program | Entire program | None | Data segment |
Static (local) | Inside block | Entire program | static | Data segment |
Static (global) | Current file only | Entire program | static | Data segment |
Extern | Cross-file access | Entire program | extern | Data segment |
🔄 Analogy: Library Books 📚
Variable Type | Analogy |
|---|---|
Local Variable | Book you borrow for a day (return after reading) |
Static Variable | A personal diary you keep adding entries to |
Global Variable | A notice board accessible to everyone in the library |
Extern Variable | A shared online document from another building |
🧠 Golden Tips
If two variables have the same name, the local variable will shadow the global one inside the function.
Using
staticinside a function preserves values between calls — very useful for counting, caching, etc.Always initialize variables — especially static and global ones.