C Scope Complete Tutorial

In this section, we will learn what the scopes are and how to use them in C.

What is a Scope? (Scope of Variables in C)

When we create an identifier (variable) in a program, it has an area where we can access and work with that variable. This area is called the scope of the identifier (variable).

Outside of this scope, it’s like the variable didn’t exist at all!

Let’s run a simple example to study the scope better:

#include <stdio.h>

void secondFunction(void);

int main() {

    int i = 100;
    
    secondFunction();
    return  0;
}

void secondFunction(void){
    
    printf("The value of the i-variable is: %d", i);
}

Output:

error: 'i' undeclared (first use in this function)

printf("the value of the i-variable is: %d", i);

In this example, within the body of the `main` function, we’ve declared the variable `i`. Then on the next line in the `main` function we called the `secondFunction` and in that function we tried to get the value assigned to the `i` variable.

But the moment we tried to compile the program, compiler returned an error which says the `i` variable is undeclared in the scope of the `secondFunction`!

This is because the `scope` of the `i` variable is limited to the `main` function’s body and it cannot be accessed outside of its scope.

The scope of the variable `i` here in this example is `function scope` which you’ll learn about it in just a bit.

Types of Scope in C

Depending on the location that a variable is declared, it can have one of these scopes:

  • File Scope
  • Function Prototype Scope
  • Function Scope
  • Block Scope

File Scope: C Global Variables

A variable that is declared outside of any function in a program has the file scope.

Any variable with such scope is accessible within any function of that program and so its value can be accessed and changed in those functions.

Example: global variables in C

#include <stdio.h>

void secondFunction(void);

int i = 100;
int main() {

    secondFunction();

    i = 400;
    secondFunction();
    return  0;
}

void secondFunction(void){

    printf("The value of the i-variable is: %d\n", i);
}

Output:

The value of the i-variable is: 100

The value of the i-variable is: 400

In this example, we’ve declared and initialized the variable `i` outside of any function and so the scope of the variable is considered `file scope`. So this variable is accessible within the body of any function of this program.

In the `main` function, we first called the `secondFunction` in order to get the value of the `i` variable and the function simply sent the value to the output without any error. After that, within the `main` function we accessed the `i` variable and changed its value to 400 and once again called the `secondFunction` in order to print the value of this variable on the monitor’s display and as you can see from the output, the value is changed to 400.

So this example shows that a variable with `file scope` is accessible within the body of any function in that program.

Function Prototype Scope

If we declare a variable as the parameter of a function’s prototype, that variable has the `function prototype scope`.

Such a variable is only accessible from the point of declaration to the end of the function’s declaration.

Basically, we can’t access the variable anywhere other than the prototype itself.

That’s why the name of a variable in a function’s prototype is optional.

Example: creating prototype scope variable in C

#include <stdio.h>

void secondFunction(int age);

int main() {

    secondFunction(33);

    return  0;
}

void secondFunction(int num){

    printf("the value of the age-variable is: %d\n", age);
}

Output:

error: 'age' undeclared (first use in this function)

printf("the value of the age-variable is: %d\n", age);

The moment we compile this example; we will get the error mentioned in the output. This is because we’re trying to access the variable `age` outside of its scope.

Function Scope: C Local variables

The scope of a defined function starts from its open brace (also called curly bracket) and ends with its closing brace.

Any variable declared in this area will have the `function scope` and such a variable can only be accessible within the function it is being declared.

Example: local variables in C

#include <stdio.h>

void secondFunction(int age);

int main() {

    int i = 10; 
    int b = 400; 

    return  0;
}

void secondFunction(int num){

    
}

In this example, the `i` and `b` variables are only accessible within the body of the `main` function because these two variables are declared within this function and so have the `function scope`. If we attempted to access the value of those variables in the body of `secondFunction`, we would’ve got a compile time error.

Note: the function parameters, even though they are created before the opening brace of a function, they have the function scope and hence belong to the block containing the function body.

Block Scope

A variable that is defined inside a block like `for-loop`, `while-loop` etc. (basically within a pair of braces `{}`) has the block scope and it is visible from the point it is defined until the end of the block containing the definition.

Example: block scope variables in C

#include <stdio.h>

int main() {

    for (int i = 0; i< 10; i++){
        continue;
    }

    printf("The value of the i-variable is: %d", i);
    return  0;
}

Output:

error: 'i' undeclared (first use in this function)

printf("The value of the i-variable is: %d", i);

As you can see, the variable `i` in this example is `block scope` and is only accessible within the block of `for loop`. And because we attempted to access the variable outside of this block, we got the error mentioned in the output.

Hiding (Shadowing) Variables

Hiding or shadowing a variable happens when we have a file-scope variable and declared a function-scope or block-scope variable with the same name in that program.

In such case, if we call the file-scope variable inside the function that has the variable with the same name; we will get the value assigned to the function-scope or block-scope variable instead of the file-scope variable.

Example: shadowing variables in C

#include <stdio.h>

int i = 500;
int main() {

    int i = 100;

    printf("The value of the i-variable is: %d", i);
    return  0;
}

Output:

The value of the i-variable is: 100

As you can see, calling the variable `i` returned the value 100 (the value assigned to the local variable) instead of the value assigned to the external variable `i`.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies