Skip to content

C Scope Rules

Scope determines where variables and functions are accessible.

1. Block Scope

c
void func() {
    int x = 10;  // block scope
    {
        int y = 20;  // inner block scope
    }
    // y is not accessible here
}

2. File Scope

c
int global_var = 100;  // file scope
static int file_var = 200;  // file scope, internal linkage

3. Function Scope

c
void func(int param) {  // parameter has function scope
    // param is accessible throughout the function
}

4. Prototype Scope

c
void func(int x);  // x has prototype scope only

5. Name Hiding

c
int x = 10;  // global

void func() {
    int x = 20;  // hides global x
    ::x = 30;  // access global x (in C++)
}

Content is for learning and research only.