Functions
C Variable Scope
A scope is a region of the program, and the scope of variables refers to the area of the program where the variables can be accessed after its declaration.
n C programming, variable declared within a function is different from a variable declared outside a function.
C programming variables can be declared in three places:
Position | Type |
---|---|
Inside a function or a block. | local variables |
Out of all functions. | Global variables |
In the function parameters. | Formal parameters |
Local Variables
Variables that are declared within the function block and can be use only within the function is called local variables.
Example:
#include <stdio.h>
int main ()
{
/* local variable definition and initialization */
int x,y,z;
/* actual initialization */
x = 20;
y = 30;
z = x + y;
printf ("value of x = %d, y = %d and z = %d\n", x, y, z);
return 0;
}
Global Variables
Variables that are declared outside of a function block and can be accessed inside the function is called global variables.
Example:
#include <stdio.h>
/* global variable definition */
int z;
int main ()
{
/* local variable definition and initialization */
int x,y;
/* actual initialization */
x = 20;
y = 30;
z = x + y;
printf ("value of x = %d, y = %d and z = %d\n", x, y, z);
return 0;
}
Comments
Post a Comment