/* File: local.c
A program to demonstrate local variables.
*/
#include
/* Function Prototypes */
void RedFish (int i);
void BlueFish (int n);
main ()
{
int n;
n = 17;
printf("Before calling RedFish, n = %d\n", n);
RedFish (32);
printf("After calling RedFish, n = %d\n", n);
printf("\n");
printf("Before calling BlueFish, n = %d\n", n);
BlueFish (n);
printf("After calling BlueFish, n = %d\n", n);
}
/* The function RedFish illustrates the idea of
local variables
*/
void RedFish (int i)
{
int n;
i = 2;
n = 14;
printf(" In RedFish, i = %d, n = %d\n", i, n);
return;
}
/* The function BlueFish illustrates the idea of
local variables
*/
void BlueFish (int n)
{
n = 72 ;
printf(" In BlueFish, n = %d\n", n);
return ;
}
The Sample Run
Before calling RedFish, n = 17
In RedFish, i = 2, n = 14
After calling RedFish, n = 17
Before calling BlueFish, n = 17
In BlueFish, n = 72
After calling BlueFish, n = 17
The Lesson
Changes to a local variable are local to the function. They do
not effect variables in the main function or in any other function.