Scope of Variables in C
Can a variable made inside one function be used in another? It depends on where you create it.
A variable made inside a function is local: it belongs only to that function. A variable made outside all functions is global: every function can use it. In this lesson you will learn the scope (where it can be used) and lifetime (how long it lives) of both.
What You Will Learn
- What scope and lifetime mean.
- Local variables and block scope.
- Global variables.
- Default values of local and global variables.
- What happens when a local and a global variable have the same name.
Scope of Variables in C Explained
Exam definition: The scope of a variable is the part of the program where it can be used. The lifetime is how long it stays in memory. A local variable is declared inside a function or block; a global variable is declared outside all functions.
| Local variable | Global variable | |
|---|---|---|
| Declared | Inside a function or block { } | Outside all functions (usually at the top) |
| Scope | Only inside that function or block | The whole file, after its declaration |
| Lifetime | Created when the block starts, destroyed when it ends | The whole program run |
| Default value | Garbage (if not initialized) | 0 |
| Stored in | Stack memory | Data segment |
Memory trick: A local variable is like a room light: only that room uses it, and it switches off when you leave. A global variable is like the sun: everyone can use it, and it is there all day (the whole program).
Block scope
Any pair of curly braces { } makes a block. A variable declared inside a block exists only inside it:
{
int x = 5; /* x exists only inside these braces */
}
/* x does not exist here */
Same name: local wins
If a local variable has the same name as a global one, the local variable hides the global one inside its function. This is called shadowing.
Syntax: Where to Declare
int count = 0; /* global: outside all functions */
void show(void)
{
int temp = 10; /* local to show() */
}
int main(void)
{
int marks = 90; /* local to main() */
return 0;
}
Example 1: Local and Global Variables Together
#include <stdio.h>
int visitors = 0; /* global variable */
void new_visitor(void)
{
int gift = 1; /* local to new_visitor() */
visitors = visitors + 1;
printf("Visitor %d gets %d gift\n", visitors, gift);
}
int main(void)
{
new_visitor();
new_visitor();
new_visitor();
printf("Total visitors: %d\n", visitors);
return 0;
}
Output
Visitor 1 gets 1 gift Visitor 2 gets 1 gift Visitor 3 gets 1 gift Total visitors: 3
Code Explanation
int visitors = 0;is global, so bothnew_visitor()andmain()can use it.- Each call to
new_visitor()increasesvisitors, and the new value stays, because a global variable lives for the whole program. int gift = 1;is local. It is created fresh (as 1) on every call and destroyed when the function ends.main()cannot usegift; it only exists insidenew_visitor().
How Scope and Lifetime Work
- When a function is called, space for its local variables is made on the stack.
- When the function returns, that space is freed. The local variables are gone.
- Global variables are created before
main()starts and stay until the program ends. - When the compiler sees a name, it looks in the innermost block first, then outward, and finally at the globals. That is why the local one wins when names are the same.
Example 2: Same Name, Local Hides Global
#include <stdio.h>
int x = 10; /* global x */
void show_global(void)
{
printf("In show_global(): x = %d\n", x);
}
int main(void)
{
int x = 5; /* local x hides the global x */
printf("In main(): x = %d\n", x);
show_global();
return 0;
}
Output
In main(): x = 5 In show_global(): x = 10
Inside main(), the local x (5) is used. show_global() has no local x, so it uses the global one (10).
Important Points to Remember
- Scope = where it can be used. Lifetime = how long it lives.
- Local: inside a function/block, garbage default value.
- Global: outside all functions, default value 0.
- Same name: the local variable wins inside its block.
- Use global variables only when really needed; any function can change them by mistake.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
| Using a local variable of one function in another | Error: 'name' undeclared | Pass it as a function argument, or make it global |
| Using a variable after its block ends | Error: undeclared | Declare it in the outer block |
| Expecting a local variable to keep its value between calls | It restarts every call | Use static (next lesson) or a global |
| Making everything global | Hard-to-find bugs | Prefer local variables |
Local and Global Variables Practice Questions
- What is the difference between a local and a global variable?
- What is the default value of an uninitialized global variable?
- What is block scope?
- What happens when a local and a global variable have the same name?
- Where is a local variable stored in memory?
Show answers
- A local variable is declared inside a function and used only there; a global variable is declared outside all functions and can be used everywhere.
- 0.
- A variable declared inside
{ }can only be used inside those braces. - The local variable hides (shadows) the global variable inside its block.
- On the stack.
Coding Exercise
Make a global variable balance = 1000. Write a function deposit() that adds 500 and a function withdraw() that subtracts 200. Call both from main() and print the final balance.
Show solution
#include <stdio.h>
int balance = 1000;
void deposit(void)
{
balance = balance + 500;
}
void withdraw(void)
{
balance = balance - 200;
}
int main(void)
{
deposit();
withdraw();
printf("Final balance: %d\n", balance);
return 0;
}
Final balance: 1300
Quick Quiz: Local and Global Variables (MCQ)
-
The default value of a global
intis:A) Garbage B) 0 C) 1 D) −1
-
A local variable is destroyed:
A) Never B) When the program ends C) When its block ends D) After 10 seconds
-
A variable declared outside all functions is:
A) Local B) Global C) Block D) Register
-
If a local and a global variable have the same name, inside the function:
A) Global is used B) Local is used C) Error D) Both are added
-
Local variables are stored in the:
A) Stack B) Hard disk C) Data segment D) Registers only
Show answers
- B: 0.
- C: when its block ends.
- B: global.
- B: the local variable.
- A: the stack.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Scope | Where a variable can be used |
|---|---|
| Lifetime | How long it stays in memory |
| Local default | Garbage |
| Global default | 0 |
| Local stored in | Stack |
| Global stored in | Data segment |
| Same name | Local hides global (shadowing) |
Frequently asked questions
- What is the difference between scope and lifetime?
- Scope is where a variable can be used; lifetime is how long it exists.
- Why should we avoid too many global variables?
- Any function can change them, which makes bugs hard to find.
- What is variable shadowing?
- When a local variable hides a global variable with the same name.
- Why is a global variable's default value 0?
- Global variables are stored in a memory area that is filled with zeros before the program starts.
Remember
- Local = room light; Global = sun.
- Default: local garbage, global 0.
Next Topic
Next lesson: Storage Classes in C. You will learn the four storage classes that control a variable's scope, lifetime and memory.