Variable Scope

In C programming, variables declared within a function are local to the function. Specifically, they’re the auto storage class, the default. External, or global, variables are defined outside of a function declaration and are of the extern storage class. These variables are available to all functions. But the scope within a function can also be limited, depending on where the variable is declared.

For a review of storage class specifiers, refer to my 2018 post on the auto keyword. Or don’t. The point is that an auto variable’s scope is local to its function and that the variable’s value is discarded when the function terminates. Further, an auto variable’s scope is also limited to the block statements in which it’s declared. This limitation may throw you for a loop [sic].

For my coding style, variable scope within a function or block isn’t an issue. I declare all variables at the top of the function, immediately after the first brace. For example, here are the first few lines of code from this month’s Exercise solution:

int main()
{
    /* constants to set seconds in a minute and seconds in an hour */
    const int minutes = 60;
    const int hours = 60 * minutes;
    int h,m,s;

These statements list all the constants and variables used throughout the main() function.

It’s also possible in C to declare a variable as it’s needed. For example:

for( int i = 0; i<100; i++ )

The statement above declares variable i as it's used in the for loop. As this declaration happens within the function's main block, variable i is available within that block. But if you define a block within a function and declare a variable within that block, the variable is visible only within the block; its scope is limited.

Consider this code:

2022_02_19-Lesson.c

#include <stdio.h>

int main()
{
    int a = 10;

    printf("Variable a = %d\n",a);
    {
        int b = a+5;
        printf("Variable b = %d\n",b);
        int c = b+a;
        printf("Variable c = %d\n",c);
    }
    int d = a+b/c;
    printf("Variable d = %d\n",d);

    return(0);
}

This program won't build. Two errors are generated because variables b and c referenced at Line 14 don't exist. These variables are declared in a block and they remain local to the block, invisible to the rest of the function.

The sample code above shows a lonely block without a control statement, which is possible but rare inside a function. More common are loop and decision blocks. If you declare a variable within such a block, it remains local (auto) to the block. The rest of the function cannot access the variable.

No solution exists to get around a variable's block-scope; you cannot declare such a variable static or extern lest the compiler have a hissy fit. The only solution I can present is to declare all variables at the top of the function, which is what I do. This approach may not seem convenient or be popular, but it avoids any problems found from declaring a variable inside a block.

Leave a Reply