Published: Sunday, December 17, 2023, Updated: Sunday, December 17, 2023

Static Scoping vs Dynamic Scoping in C language with Example

In programming languages, including C, scoping rules define the visibility and accessibility of variables in different parts of a program. 

There are two main types of scoping: static scoping (also known as lexical scoping) and dynamic scoping. 

Let's discuss both concepts with examples in the context of the C programming language.

1. Static Scoping (Lexical Scoping)

In static scoping, the scope of a variable is determined at compile-time, and it depends on the block structure of the program. Variables are bound to their scope based on where they are declared in the source code.

#include <stdio.h>

void outer_function() {
    int x = 10;

    void inner_function() {
        printf("Static Scoping: x = %d\n", x);
    }

    inner_function();
}

int main() {
    outer_function();

    // Uncommenting the next line would result in a compilation error
    // inner_function(); // Error: 'inner_function' undeclared
    return 0;
}

In this example, the variable x is declared in the outer_function, and the inner_function has access to it because it is lexically enclosed within the scope of outer_function. Trying to call inner_function outside of outer_function results in a compilation error because it's not in the scope.

Easier Example of Static Scoping

#include <stdio.h>

void outer_function() {
    int x = 10;
    printf("Static Scoping: x = %d\n", x);
}

int main() {
    outer_function();

    // Uncommenting the next line would result in a compilation error
    // printf("Static Scoping: x = %d\n", x); // Error: 'x' undeclared

    return 0;
}

In this example, the variable x is declared in the outer_function, and it is not accessible outside that function's scope. Uncommenting the line in main attempting to print x would result in a compilation error.

2. Dynamic Scoping

Dynamic scoping determines the scope of a variable at runtime based on the call stack. The scope is determined by the sequence of function calls, and variables are looked up in the call stack.

Dynamic scoping is not natively supported in C, but you can illustrate the concept with a similar example using global variables and function calls.

#include <stdio.h>

int x = 10;

void outer_function() {
    printf("Dynamic Scoping: x = %d\n", x);
}

void inner_function() {
    printf("Dynamic Scoping: x = %d\n", x);
}

int main() {
    outer_function();

    // Change the value of x
    x = 20;

    inner_function();

    return 0;
}

In this example, the functions outer_function and inner_function both have access to the global variable x. The output depends on the sequence of function calls at runtime.

Easier Example of Dynamic Scoping

#include <stdio.h>

int x = 10;

void dynamic_function() {
    printf("Dynamic Scoping: x = %d\n", x);
}

int main() {
    dynamic_function();

    // Change the value of x
    x = 20;

    dynamic_function();

    return 0;
}

In this example, the variable x is a global variable, and both dynamic_function calls have access to it. Changing the value of x in one part of the program affects the value seen by the other part at runtime.

Note: Dynamic scoping is not a native feature in C, and the example provided is a simulation using global variables. C primarily relies on static scoping, and dynamic scoping is more commonly associated with languages like Lisp or Perl.

Elliyas Ahmed
Elliyas Ahmed
Elliyas Ahmed, founder of COMPROMATH, is a Computer Science and Engineering graduate who excels in Blogging, Website Development, and SEO. He offers freelancing services to help clients succeed online. Learn more about Elliyas Ahmed.