Variables that are declared inside a function are called local variables. Local variables can be used 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. 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. Furthermore, a variable declared within one code block has no bearing on or relationship to another variable with the same name declared within a different code block.
Example:
void func1(void) { int y; y = 10; } void func2(void) { int y; y = -199; } |
The integer variable y is declared twice, once in func1( ) and once in func2( ). The y in func1( ) has no relationship to the y in func2( ). This is because each y is known only to the code within the block in which it is declared.
The C language contains the keyword auto, which can be used to declare local variables. However, since all variables are, by default, assumed to be auto, this keyword is virtually never used.