Variable Scope in C Programming

In C, the scope of a variable refers to the region of the code where the variable can be accessed. Understanding scope is crucial for effective programming and avoiding issues like variable shadowing or unintended modifications. Here are the main types of variable scope in C:

1. Local Variable Scope

Local variables can be stored in functions or blocks. To understand function or block-level scope, let’s look at the following examples.

a) Function Scope

  • Variables defined within a function have function scope.
  • They can be accessed anywhere within that function, but not outside of it.
  • Example:
void myFunction1() {
    int a = 5; // a has function scope and can be accessed throughout the the "myFunction1" function
}
void myFunction2() {
     // a is not accessible here
}

b) Block Scope

Variables defined inside a block (enclosed in curly braces {}) have block scope. They are only accessible within that block.

Example:

void function() {
    int x = 10; // x has entire function scope
    if (x > 5) {
        int y = 20; // y has block scope within this if statement
    }
    // y is not accessible here
}

2. Global Scope

Global variables are accessible in any function or any file.

Keep in mind: if you want to use the Global variable in another file then you must declare it with extern keyword in that file.

file1.c

#include <stdio.h>
 int globalVar = 10;  // Definition of the global variable 
void display() {
    printf("Global Variable: %d\n", globalVar);
}

file2.c

#include <stdio.h>
// Declaration of the global variable
extern int globalVar;
void modify() {
globalVar += 5; // Modifying the global variable
}
int main() {
printf("Initial Global Variable: %d\n", globalVar);
modify();
printf("Modified Global Variable: %d\n", globalVar);
return 0;
}

3. Static Scope

1. Static Local Variables

A local static variable retains its value between function calls. It is initialized only once, and its scope is limited to the function in which it is defined.

Static keyword Without static keyword
#include <stdio.h>

void myfun() {
static int count = 0; // Local static variable
count++;
printf(“Count: %d\n”, count);
}

int main() {
for (int i = 0; i < 5; i++) {
myfun(); // Call the function multiple times
}
return 0;
}

Output

Count: 1 Count: 2 Count: 3 Count: 4 Count: 5

#include <stdio.h>

void myfun() {
int count = 0; // Local variable
count++;
printf(“Count: %d\n”, count);
}

int main() {
for (int i = 0; i < 5; i++) {
myfun(); // Call the function multiple times
}
return 0;
}

Output

Count: 1Count: 1Count: 1Count: 1Count: 1 

 

2. Static Global Variables

A global static variable is accessible only within the file in which it is declared. It cannot be accessed from other files, even if they declare it using extern.

#include <stdio.h>
static int globalCount = 0; // Global static variable
void increment() { 
globalCount++;
}
void display() { 
printf("Global Count: %d\n", globalCount);
}
int main() { 
increment(); 
increment(); 
display(); // print 2 
return 0;
}

Output

 Global Count: 2

Keep in mind:

  • Both global static and global non-static variables retain their values throughout the program’s execution.
  • With static keyword globalCount variable can only be accessed within the file in which it is defined but without static keyword it becomes an externally visible global variable.