10.2 – Internal or Local variables in C

by subbu on November 24, 2013

Internal or Local variables in C:

Variables declared within a function are called local variables. These variables are stored in the memory stack separately for every function. Even, the block variables in a function are local variables.

Scope of local variables:

The visibility of these variables is limited to the home function in which they are declared, local variables belongs to one function can’t be accessed from another function.
Say for example, variables declared within main() are local to main(), can’t be  accessed directly from other function like display(). In the same way, variables belongs to display() can’t be directly accessed from the main().

Example:

/* scope of automatic variable */
#include<stdio.h>
int main()
{
 int x=10;
 display();
 return 0;
}
void display()
{
 printf("x=%d",x);
}

Output:
Error: undefined symbol “x” in function display()

Example explained:
scope of automatic variableHere “x” is the local variable to main(), can’t be accessed from display() because the scope of “x” is limited to the main() function only

Example:

/* scope of automatic variable */
#include<stdio.h>
int main()
{
 int x=40;
 printf("x=%d",x);
 process();
 printf("\nx=%d",x);
 return 0;
}
void process()
{
 int x=80;
 printf("\nx=%d",x);
}

Output:
x=40
x=80
x=40

Example explained:
Scope of local variablesHere “x” is declared for two times that is in main() and again in process(). They are different memory locations; visibility of which is limited to their home functions. Variable belongs to one function can’t be accessed from another function.

Life of local variables:

Local variables in a function come into existence only when a function is called, disappears on the completion of function execution. Local variables are stored in a place called Stack Segment in the computer memory and local to the home function. As the memory allocation of local variables allocate (born) on entering the control into the function and de-allocate (dead) on completion of function execution automatically, these variables are called automatic variables.

Automatic variables of a function do not keep their values from one execution to another of a function because they loose their life at the end of every execution

Stack segment of memory

Example:

/* life of local variables */
#include<stdio.h>
void display();
int main()
{
 display();
 display();
 display();
 return 0;
}
void display()
{
 int x=10;
 x=x+10;
 printf("\n%d",x);
}

Output:
20
20
20

Example explained:
Here in this example, x is a local variable to the function display(), doesn’t keep its values for the next execution because it looses its life at the end of every execution
Hence, for every execution x is allocated and assigned with 10. So, the result would be same for every call.

Example:

/* life of local variables */
#include<stdio.h>
void display(int);
int main()
{
 int i;
 for(i=1;i<=5;i++)
  display(i);
 return 0;
}
void display(int x)
{
 int a=100;
 a=a+x;
 printf("\t%d",a);
}

Output:
101     102     103     104     105

Example explained:
Life of automatic variablesHere in this example, “a” is a local variable to the function display(), memory allocation of which is allocated at the beginning of function execution and de-allocates on completion of function execution.
“a” is reborn and assigned with 100 for every execution.

Default value of an automatic (local) variable:

A local or automatic variable is assigned with a garbage value, if not assigned with any value.

Example:

#include<stdio.h>
int main()
{
 int x;
 printf("%d",x);
 return 0;
}

Output:
42987  (Garbage value)

When do these die?

As we know variables declared in a function are called local variables. These come into existence on entering the control into the function and disappear on completion of function execution.
But, what happens to the variables of main()?
We know that, even main() is a user defined function; It is the entry and exit point to any program. The memory allocation of main() variables allocate at the beginning of program execution and de-allocate on completion of program execution. Hence the life of main() variables is along the program.

Conclusion

  • Variables declared within a function are called automatic or local variables
  • Scope of local variables is within the function, variables belongs to one function can’t be accessed from other function
  • Local variables appear as the control enters into the function, disappears as the function execution completes automatically. So called automatic variables
  • Local variables store in the stack segment of memory
  • The default value of a local variable is the garbage value

Previous post:

Next post: