QP School

Full Version: Global and Local scope
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Variables declared outside any function have global scope and can be accessed throughout the program. Variables declared inside a function have local scope and can only be accessed within that function.

int global_var = 10; // Global variable

void my_function() {
    int local_var = 5; // Local variable
    printf("Global variable: %d\n", global_var);
    printf("Local variable: %d\n", local_var);
}

int main() {
    my_function();
    return 0;
}