Local Variables in C

In this lesson we will see how to declare local variables in C language. A local variable is a variable declared inside the body of a function.

A local variable is visible only inside the function in which it is declared. This means that a local variable cannot be used outside the function itself.

Local Variables

When we declare a variable in the body of a function, that variable takes the name of local variable of the function.

For example, suppose we want to create a sum function that returns the sum of the first n positive integers. The sum function will have a parameter n that indicates the number of positive integers to sum. The sum function could be implemented as follows:

int sum(int n) {
    int i;
    int sum = 0;
    for (i = 1; i <= n; i++) {
        sum += i;
    }
    return sum;
}

In this example, the variables i and sum are local variables of the sum function. These variables are visible only inside the function and cannot be used outside of it.

In fact, if we tried to use the variable i outside the sum function, the compiler would signal an error:

int main() {
    int n = 10;
    int s = sum(n);
    printf("The sum of the first %d positive integers is %d\n", n, s);
    printf("The variable i is worth %d\n", i);
    return 0;
}

Trying to compile the program with the command gcc local-variables.c -o local-variables we will get the following error:

local-variables.c: In function 'main':
local-variables.c:7:49: error: 'i' undeclared (first use in this function)
     printf("The variable i is worth %d\n", i);
                                        ^
local-variables.c:7:49: note: each undeclared identifier is reported only once for each function it appears in 
Definition

Local Variables

A Local Variable in C language is any variable defined inside the body of a function. Local variables are visible only inside the function in which they are declared and cannot be used outside of it.

Often these variables are also called Automatic Variables.

Properties of Local Variables

Local variables have some properties:

Definition

Automatic Lifetime

The lifetime of a variable, in English Storage duration is the portion of program execution during which the memory (storage) of the variable exists and is valid.

A local variable has an automatic lifetime in the sense that the memory for the variable is allocated when the function is called and is freed when the function terminates. From this derives the fact that such variables are also known by the name of Automatic variables.

This concept of automatic lifetime will be clearer when we study, in the next lessons, the concept of Function Call Stack.

Definition

Limited Visibility

The visibility, in English Scope, of a variable is that portion of program in which the variable can be accessed.

In the case of a local variable the visibility is limited to the body of the function in which it is declared. This means that a local variable cannot be used outside the function in which it is declared. It will be visible starting from the point where it is declared until the end of the body of the function itself.

Local Variables and C99

Since in the C99 standard it is possible to declare a variable even after the beginning of the function in which it is declared, it is possible to declare a local variable even after executing other instructions.

For example, we might want to implement a function that performs the sum of the first n positive integers and calculates the square of this value. The function could be implemented as follows:

int sum_square(int n) {
    int i;
    int sum = 0;
    for (i = 1; i <= n; i++) {
        sum += i;
    }
    int square = sum * sum;
    return square;
}

In this function we declared the variable square after executing the for loop. The scope or visibility of the variable square is limited to the last two instructions of the sum_square function.

Static Local Variables

Static local variables are local variables that maintain their value even after the termination of the function in which they are declared. This means that the memory for the static local variable is allocated only once and is freed only at the end of the program execution.

Static local variables are useful when you want to maintain the value of a local variable even after the termination of the function in which it is declared.

For example, suppose we want to implement a count function that counts the number of times it is called. The count function could be implemented as follows:

int count() {
    static int counter = 0;
    counter++;
    return counter;
}

In this function we declared the variable counter as a static local variable. This variable is initialized at the first call of the count function and is incremented every time the count function is called. This means that the variable counter maintains its value even after the termination of the count function. To do this we used the keyword static before the declaration of the variable counter.

If we try to use the count function as follows:

int main() {
    int i;
    for (i = 0; i < 10; i++) {
        printf("The count function has been called %d times\n", count());
    }
    return 0;
}

We will get the following output:

The count function has been called 1 times
The count function has been called 2 times
The count function has been called 3 times
The count function has been called 4 times
The count function has been called 5 times
The count function has been called 6 times
The count function has been called 7 times
The count function has been called 8 times
The count function has been called 9 times
The count function has been called 10 times

Unlike normal local variables, a static local variable has a static lifetime. This means that the memory for the static local variable is allocated only once and is freed only at the end of the program execution.

However, the visibility of a static local variable is always limited to the body of the function in which it is declared. Therefore, like a normal local variable, a static local variable cannot be used outside the function in which it is declared.

Definition

Static Local Variables

Static local variables are local variables that maintain their value even after the termination of the function in which they are declared.

To declare a static local variable the keyword static is used before the declaration of the variable itself:

static type variable_name;

The visibility of a static local variable is limited to the body of the function in which it is declared and cannot be accessed from outside.

Local Variables and Parameters

The parameters of a function have the same identical properties as local variables. In particular, the parameters of a function have an automatic lifetime and a limited visibility.

In fact, the only difference between the parameters of a function and local variables is that the parameters of a function are declared in the parameter list of the function and are initialized from the arguments passed to the function when it is called.

Definition

Parameters and Local Variables

A parameter of a function is to all effects a local variable of the function itself. The parameters of a function have the same properties as local variables:

  • Automatic lifetime: the memory for the parameters of a function is allocated when the function is called and is freed when the function terminates.
  • Limited visibility: the visibility of the parameters of a function is limited to the body of the function in which they are declared.

In Summary

In this chapter we learned:

  • That local variables are variables declared inside the body of a function.
  • That local variables have an automatic lifetime and a limited visibility.
  • That static local variables are local variables that maintain their value even after the termination of the function in which they are declared.
  • That the parameters of a function have the same properties as local variables.

In the next lesson we will introduce the concept of External Variables or often called Global Variables.