Local variables are declared within a function definition and could only be accessed from anywhere in that function in which it is declared.
Thus, local variables have their scope local to the function in which they are declared and are unknown to other functions outside their own function.
Example :
#include <stdio.h>
int main ()
{
/* local variable declaration */
int x, y, z;
/* actual initialization */
x = 1;
y = 2;
z = x + y;
printf ("Value of x = %d, y = %d and z = %d\n", x, y, z);
return 0;
}
Output :
Value of x = 1, y = 2 and z = 3
Explanation :
Here the variable x, y, z are local variable. And the scope of these variables is within the main() function.
No comments:
Post a Comment