Skip to content
StudyFame

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 variableGlobal variable
DeclaredInside a function or block { }Outside all functions (usually at the top)
ScopeOnly inside that function or blockThe whole file, after its declaration
LifetimeCreated when the block starts, destroyed when it endsThe whole program run
Default valueGarbage (if not initialized)0
Stored inStack memoryData 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

  1. int visitors = 0; is global, so both new_visitor() and main() can use it.
  2. Each call to new_visitor() increases visitors, and the new value stays, because a global variable lives for the whole program.
  3. int gift = 1; is local. It is created fresh (as 1) on every call and destroyed when the function ends.
  4. main() cannot use gift; it only exists inside new_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

MistakeWhat happensFix
Using a local variable of one function in anotherError: 'name' undeclaredPass it as a function argument, or make it global
Using a variable after its block endsError: undeclaredDeclare it in the outer block
Expecting a local variable to keep its value between callsIt restarts every callUse static (next lesson) or a global
Making everything globalHard-to-find bugsPrefer local variables

Local and Global Variables Practice Questions

  1. What is the difference between a local and a global variable?
  2. What is the default value of an uninitialized global variable?
  3. What is block scope?
  4. What happens when a local and a global variable have the same name?
  5. Where is a local variable stored in memory?
Show answers
  1. 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.
  2. 0.
  3. A variable declared inside { } can only be used inside those braces.
  4. The local variable hides (shadows) the global variable inside its block.
  5. 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)

  1. The default value of a global int is:

    A) Garbage   B) 0   C) 1   D) −1

  2. A local variable is destroyed:

    A) Never   B) When the program ends   C) When its block ends   D) After 10 seconds

  3. A variable declared outside all functions is:

    A) Local   B) Global   C) Block   D) Register

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

  5. Local variables are stored in the:

    A) Stack   B) Hard disk   C) Data segment   D) Registers only

Show answers
  1. B: 0.
  2. C: when its block ends.
  3. B: global.
  4. B: the local variable.
  5. A: the stack.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

ScopeWhere a variable can be used
LifetimeHow long it stays in memory
Local defaultGarbage
Global default0
Local stored inStack
Global stored inData segment
Same nameLocal 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.