C Variable Scope

Beginner
6 min read
Updated: Nov 2026
1 example

What Is Variable Scope?

Scope is the region where an identifier can be used. A variable declared in a block is visible from its declaration to the closing brace (block scope); one declared outside all functions has file scope. An inner declaration can shadow an outer one.

Remember
int g = 100;            /* file scope  */
void f(void) {
    int x = 1;          /* block scope */
    { int x = 2; }      /* shadows outer x */
}

Cheat Sheet

Keep these facts handy while you work with Variable Scope in C.

TopicDetail
BlockBetween { and }
FileDeclared outside functions
FunctionOnly labels have function scope
ShadowingInner name hides outer name

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Lifetime and Linkage.

Example 1 — Shadowing and file-scope variables

A local counter hides the global one without changing it.

C
#include <stdio.h>

int counter = 100;

void show(void) {
    printf("file-scope counter: %d\n", counter);
}

int main(void) {
    int x = 1;
    {
        int x = 2;
        printf("inner x = %d\n", x);
    }
    printf("outer x = %d\n", x);

    show();
    int counter = 5;
    printf("local counter = %d\n", counter);
    show();
    return 0;
}

How It Works

  1. The inner x exists only inside its braces.
  2. Declaring a local counter hides the global in main only.
  3. show still sees the file-scope variable.

Edge Cases & Pitfalls

Check these before relying on Variable Scope in real programs.

global

Overusing globals

Globals make code hard to test and reason about. Prefer parameters and return values.

for

Loop variable scope

A variable declared in the for header (C99) is not visible after the loop.

Key Takeaways

  • Block: braces start a scope.
  • Shadow: inner names hide outer ones.
  • Limit: keep scope as small as possible.

One line: Scope is the region where an identifier can be used.

Frequently Asked Questions

Local variables live in a block and exist only while it runs. Global (file-scope) variables are visible to the whole file and persist for the entire program.
Shadowing happens when an inner declaration uses the same name as an outer one, hiding the outer variable inside the inner scope.
Continue with "C Lifetime and Linkage" next. How long an object lives and which files can see its name. The C Functions hub lists every topic in order.

Next: Lifetime and Linkage

How long an object lives and which files can see its name.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful