Tuesday, September 21, 2010

Local Variables

Variables that are declared inside a function are called local variables. In some C/C++
literature, these variables are referred to as automatic variables. This book uses the morecommon term, local variable. Local variables may be referenced only by statements
that are inside the block in which the variables are declared. In other words, local
variables are not known outside their own code block. Remember, a block of code
begins with an opening curly brace and terminates with a closing curly brace.
Local variables exist only while the block of code in which they are declared is
executing. That is, a local variable is created upon entry into its block and destroyed
upon exit.
The most common code block in which local variables are declared is the function.
For example, consider the following two functions:
void func1(void)
{
int x;
x = 10;
}
void func2(void)
{
int x;
x = -199;
}
The integer variable x is declared twice, once in func1() and once in func2() . The x in
func1() has no bearing on or relationship to the x in func2() . This is because each x is
only known to the code within the same block as the variable declaration.
The C language contains the keyword auto, which you can use to declare local
variables. However, since all nonglobal variables are, by default, assumed to be auto,
this keyword is virtually never used. Hence, the examples in this book will not use it.
(It has been said that auto was included in C to provide for source-level compatibility
with its predecessor B. Further, auto is supported in C++ to provide compatibility
with C.)
For reasons of convenience and tradition, most programmers declare all the
variables used by a function immediately after the function's opening curly brace
and before any other statements. However, you may declare local variables within any
code block. The block defined by a function is simply a special case. For example,
void f(void)
{
int t;scanf("%d%*c", &t);
if(t==1) {
char s[80]; /* this is created only upon
entry into this block */
printf("Enter name:");
gets(s);
/* do something ... */
}
}
Here, the local variable s is created upon entry into the if code block and destroyed
upon exit. Furthermore, s is known only within the if block and may not be referenced
elsewhere—even in other parts of the function that contains it.
One advantage of declaring a local variable within a conditional block is that
memory for the variable will only be allocated if needed. This is because local variables
do not come into existence until the block in which they are declared is entered. You
might need to worry about this when producing code for dedicated controllers (like a
garage door opener that responds to a digital security code) in which RAM is in short
supply, for example.
Declaring variables within the block of code that uses them also helps prevent
unwanted side effects. Since the variable does not exist outside the block in which it
is declared, it cannot be accidentally altered.
There is an important difference between C and C++ as to where you can declare
local variables. In C, you must declare all local variables at the start of the block in
which they are defined, prior to any "action" statements. For example, the following
function is in error if compiled by a C compiler.
/* This function is in error if compiled as
a C program, but perfectly acceptable if
compiled as a C++ program.
*/
void f(void)
{
int i;
i = 10;
int j; /* this line will cause an error */
j = 20;
}
However, in C++, this function is perfectly valid because you can define local variables
at any point in your program. (The topic of C++ variable declaration is discussed in
depth in Part Two.)
Because local variables are created and destroyed with each entry and exit from
the block in which they are declared, their content is lost once the block is left. This is
especially important to remember when calling a function. When a function is called,
its local variables are created, and upon its return they are destroyed. This means that
local variables cannot retain their values between calls. (However, you can direct the
compiler to retain their values by using the static modifier.)
Unless otherwise specified, local variables are stored on the stack. The fact that
the stack is a dynamic and changing region of memory explains why local variables
cannot, in general, hold their values between function calls.
You can initialize a local variable to some known value. This value will be assigned
to the variable each time the block of code in which it is declared is entered. For example,
the following program prints the number 10 ten times:
#include <stdio.h>
void f(void);
int main(void)
{
int i;
for(i=0; i<10; i++) f();
return 0;
}
void f(void)
{
int j = 10;
printf("%d ", j);
j++; /* this line has no lasting effect */
}

No comments:

Post a Comment