C Scope

Scope in C means the part of a program where a name can be used. A variable’s scope controls which functions or blocks are allowed to see that variable.

Understanding scope helps you avoid confusing bugs and keeps each function focused on its own data.

Local Scope

A variable declared inside a function is local to that function. Code outside the function cannot use that variable by name.

#include <stdio.h>

void showTotal(void)
{
    int total = 15;
    printf("Inside function: %d\n", total);
}

int main(void)
{
    int total = 40;

    showTotal();
    printf("Inside main: %d\n", total);

    return 0;
}

Output:

Inside function: 15
Inside main: 40

There are two different variables named total. The one inside showTotal belongs to that function. The one inside main belongs to main. They have the same name, but they are separate variables.

Parameters Have Local Scope

Function parameters are also local variables. A parameter exists only while that function call is running.

#include <stdio.h>

void printDouble(int number)
{
    int doubled = number * 2;
    printf("Double: %d\n", doubled);
}

int main(void)
{
    int number = 7;

    printDouble(number);
    printf("Original: %d\n", number);

    return 0;
}

Output:

Double: 14
Original: 7

The parameter number inside printDouble is a local copy of the argument from main. The variable doubled is also local to printDouble.

Global Scope

A variable declared outside all functions has global scope. It can be used by functions that appear after its declaration in the same file.

#include <stdio.h>

int counter = 0;

void addOne(void)
{
    counter = counter + 1;
}

int main(void)
{
    addOne();
    addOne();

    printf("Counter: %d\n", counter);

    return 0;
}

Output:

Counter: 2

The variable counter is declared before the functions, so both addOne and main can use it. Global variables can be useful for simple examples, but use them carefully because many functions can change the same value.

Block Scope

A block is a group of statements inside braces. Variables declared inside a block can be used only inside that block.

#include <stdio.h>

int main(void)
{
    int score = 80;

    if (score >= 60) {
        int bonus = 5;
        printf("Passing score: %d\n", score + bonus);
    }

    printf("Score: %d\n", score);

    return 0;
}

Output:

Passing score: 85
Score: 80

The variable bonus exists only inside the if block. After the closing brace, bonus is out of scope and cannot be used.

Name Hiding

If an inner scope declares a variable with the same name as an outer scope, the inner variable hides the outer one until the inner scope ends.

#include <stdio.h>

int value = 100;

int main(void)
{
    int value = 20;

    printf("Local value: %d\n", value);

    return 0;
}

Output:

Local value: 20

The local value inside main hides the global value. This compiles, but using the same name in nested scopes can make code harder to read.

Quick Rules

  • Declare variables as close as possible to where they are used.
  • Use local variables for data that belongs to one function.
  • Remember that parameters are local to their function.
  • Use global variables only when shared state is truly needed.
  • A variable declared inside braces cannot be used after that block ends.

The key idea is that scope decides where a name is visible, so clear scope makes functions easier to understand and safer to change.