C Programming Language Tutorial

Variables and Data Types

Input/Output

Looping and Selection Structures

Array

Functions

Preprocessing Command

Pointer

Structure

File Operations

Important Knowledge

Variables Scope in C Programming Language

In this tutorial, we will discuss variable scope in the C programming language. Variable scope refers to the visibility and accessibility of a variable in a program. We will cover different types of variable scope and provide examples to demonstrate their usage.

There are three types of variable scope in C:

  1. Local scope
  2. Global scope
  3. Static scope

1. Local Scope

A variable declared inside a function or a block of code is said to have local scope. These variables are also called automatic variables or local variables. They can only be accessed within the function or block where they are declared. Once the function or block is exited, the variable is destroyed and its memory is deallocated.

Example of local scope:

#include <stdio.h>

void local_example() {
    int a = 10; // a has local scope
    printf("Inside local_example, value of a: %d\n", a);
}

int main() {
    int a = 5; // a has local scope
    printf("Inside main, value of a: %d\n", a);
    
    local_example();

    printf("Inside main after calling local_example, value of a: %d\n", a);
    
    return 0;
}

In this example, there are two variables with the name a, but they have different scopes. The variable a declared in the main function can only be accessed within main, while the variable a declared in local_example can only be accessed within local_example. The output will be:

Inside main, value of a: 5
Inside local_example, value of a: 10
Inside main after calling local_example, value of a: 5

2. Global Scope

A variable declared outside of any function has global scope. Global variables are accessible from any function in the program. Their lifetime is the entire duration of the program's execution.

Example of global scope:

#include <stdio.h>

int a = 5; // a has global scope

void global_example() {
    printf("Inside global_example, value of a: %d\n", a);
}

int main() {
    printf("Inside main, value of a: %d\n", a);
    
    global_example();

    printf("Inside main after calling global_example, value of a: %d\n", a);

    return 0;
}

In this example, the variable a has global scope because it is declared outside of any function. It can be accessed from both main and global_example functions. The output will be:

Inside main, value of a: 5
Inside global_example, value of a: 5
Inside main after calling global_example, value of a: 5

3. Static Scope

Static variables have a scope similar to local variables, but their lifetime is extended to the entire duration of the program's execution. They are only accessible within the function where they are declared, but they retain their value between function calls. Static variables are declared using the static keyword.

Example of static scope:

#include <stdio.h>

void static_example() {
    static int a = 0; // a has static scope
    a++;
    printf("Inside static_example, value of a: %d\n", a);
}

int main() {
    static_example();
    static_example();
    static_example();

    return 0;
}

In this example, the variable a inside the static_example function has static scope. It retains its value between function calls, so the output will be:

Inside static_example, value of a: 1
Inside static_example, value of a: 2
Inside static_example, value of a: 3
  1. Block scope and function scope in C programming:

    • Description: Variables declared within a block (enclosed by {}) have block scope, while those declared outside any function have file scope. Function parameters also have function scope.
    • Example:
      #include <stdio.h>
      
      // Block scope and function scope in C programming
      int main() {
          int functionScopedVar = 10;
      
          if (1) {
              int blockScopedVar = 5;
              printf("Block Scoped Variable: %d\n", blockScopedVar);
          }
      
          printf("Function Scoped Variable: %d\n", functionScopedVar);
      
          return 0;
      }
      
  2. Lifetime and visibility of variables in C:

    • Description: The lifetime of a variable is the duration it exists in memory, and visibility refers to where the variable can be accessed.
    • Example:
      #include <stdio.h>
      
      // Lifetime and visibility of variables in C
      int globalVar = 42;  // File scope, visible throughout the file
      
      int main() {
          int localVar = 10;  // Function scope, visible only within main()
      
          {
              int blockVar = 5;  // Block scope, visible within the block
              printf("Block Variable: %d\n", blockVar);
          }
      
          printf("Local Variable: %d\n", localVar);
          printf("Global Variable: %d\n", globalVar);
      
          return 0;
      }
      
  3. Static variables and their scope in C language:

    • Description: Static variables have file scope with internal linkage by default, making them accessible only within the file.
    • Example:
      #include <stdio.h>
      
      // Static variables and their scope in C language
      static int staticVar = 30;  // Static variable with file scope
      
      int main() {
          printf("Static Variable: %d\n", staticVar);
      
          return 0;
      }
      
  4. Using external and file scope variables in C:

    • Description: External variables declared with extern have file scope and can be accessed across multiple files.
    • Example:
      // File1.c
      int sharedVar = 50;  // External variable with file scope
      
      // File2.c
      #include <stdio.h>
      
      extern int sharedVar;  // Declaration of external variable
      
      int main() {
          printf("Shared Variable from File1: %d\n", sharedVar);
      
          return 0;
      }
      
  5. Variable shadowing and its impact on scope in C:

    • Description: Variable shadowing occurs when a variable in an inner scope has the same name as one in an outer scope, potentially leading to confusion.
    • Example:
      #include <stdio.h>
      
      // Variable shadowing and its impact on scope in C
      int main() {
          int x = 10;
      
          if (1) {
              int x = 5;  // Inner variable shadows outer variable
              printf("Inner Variable X: %d\n", x);
          }
      
          printf("Outer Variable X: %d\n", x);
      
          return 0;
      }
      
  6. C code examples demonstrating variable scope:

    • Example:
      #include <stdio.h>
      
      // C code examples demonstrating variable scope
      int globalVar = 42;  // Global variable
      
      void myFunction() {
          int functionVar = 15;  // Function scope variable
          printf("Function Variable: %d\n", functionVar);
      }
      
      int main() {
          int mainVar = 30;  // Main function scope variable
      
          myFunction();
      
          printf("Global Variable: %d\n", globalVar);
          printf("Main Variable: %d\n", mainVar);
      
          return 0;
      }