Skip to content
Home » All Posts » The C Scope is More Important than a Variable

The C Scope is More Important than a Variable

the c scope

Introduction

Scope of variables in C is much more important than knowing how to declare a variable. So far, you should have known how to declare a variable and use it to do some operations. This is great, but knowing the scope and life time of your variable is equally as important. Let’s take a look at them.

The Local Scope

The scope of a variable refers to the accessibility of a variable in a program or a function. After you have declared a variable, consider:

  • who has access to this variable?
  • where can it be accessed?
  • what is this variable’s life time?

Consider this example:

void myfunc1(void)
{
    int a = 5;
    printf("a in myfunc1 = %d\n", a);
}

void myfunc2(int a)
{
    printf("a in myfunc2 = %d\n", a);
}
int main()
{
    myfunc1();
    myfunc2(7);

    return 0;
}

The above example produces this output:

a in myfunc1 = 5
a in myfunc2 = 7

Both functions try to print the same variable a but result in different values. This is because the variable declared inside myfunc1 is only accessible within myfunc1(); it cannot be accessed from myfunc2() or the main() function. myfunc2() does not declare any variable but it does take an argument also called a and the value 7 is passed to the function. The compile will make a copy of the value 7 and assign it to variable a inside myfunc2(), so it is equivalent to declaring a variable inside myfunc2() and similarly, it cannot be accessed from outside of myfunc2().

The variable a in the above example is also called a “local variable“, because it can only be accessed locally within the function. The life time of a local variable lasts until the end of the function. In other words, when a function returns, the variable a is destroyed and no longer accessible.

The Global Scope

Consider another example:

int a = 5;

void myfunc1(void)
{
    printf("a in myfunc1 = %d\n", a);
}

void myfunc2(void)
{
    printf("a in myfunc2 = %d\n", a);
}
int main()
{
    myfunc1();
    a = a + 1;
    myfunc2();
    
    return 0;
}

The above example produces this output:

a in myfunc1 = 5
a in myfunc2 = 6

Both myfunc1() and myfunc2() print the value of a which is declared on the very top outside of any function block. This differs from “local variable” declared within a function in terms of the scope. All functions within the same source file have access to the variable a. The above example first prints the value of a (5), then adds 1 to it in the main() function, and finally myfunc2() prints the same variable, which is now 6.

The variable a in this example is also called a “global variable” accessible by all functions within the source file. Its life time lasts until the program exits

The Nested Scope

It is possible to have nested scope within a local scope (within a function). It is generally not considered a good practice because it makes the code confusing to read. Yet, it is still important to know the principles of C scopes. Consider this example:

int main()
{
    int a = 5;
    printf("1: a = %d\n", a);
    
    if (a == 5)
    {
        int a = 10;
        printf("2: a = %d\n", a);
    }
    
    {
        int a = 20;
        printf("3: a = %d\n", a);
    }
    printf("4: a = %d\n", a);

    return 0;
}

The above example produces this output:

1: a = 5
2: a = 10
3: a = 20
4: a = 5

There are multiple declarations of local variable a within the main() function, but they all have different values even though they all have the same name. This is again related to the different scope declarations of variable and where the declaration happens. It follows this general rules about scope of variables in C below:

  • The variable declared when the function begins are accessible anywhere within the function.
  • The variable declared inside an “if statement” are accessible within the function “if statement” only, not outside of it. This also applies to “while loop”, “for loop”, or any other clauses that uses curly brackets {} to enclose.
  • The variable declared inside curly brackets {} are accessible only within, not outside.

Consider another example below:

int main()
{
    int a = 5;
    if (a == 5)
    {
        int b = 10;
        printf("a = %d\n", a);
    }
    printf("b = %d\n", b);
    return 0;
}

The example above will result in compilation error at line 9 because variable b is not declared even though it is declared in line 6. This is once again due to the scope of variables in C. The variable declared inside the “if statement” is only accessible within that “if statement”. Likewise, the variable a is declared when the function begins, so it can be accessed within the “if statement” at line 7 and anywhere else within this function.

Mixture of Local and Global Scopes

What if you have a variable declared both locally and globally? Consider this example below:

int a = 5;
int main()
{
    int a = 10;
    if (a == 10)
    {
        int a = 20;
        printf("a = %d\n", a);
    }
    return 0;
}

What should the value of a be when we try to print it on line 8? Would it print the value of 20 (local) or 5 (global)? The above example produces this output:

a = 20

Yes, it prints the local variable declared within the “if statement”, not the variable declared globally even though they both have the same name. Keep it in mind, the local variable takes precedence over the global one in the case of a name conflict.

Other Recommended Reads

Join the conversation

Your email address will not be published. Required fields are marked *