VARIABLES IN C
‘C’ programs are variables, constants, arithmetic operator etc. A variable in a program is a name to which you can associate a value.The variables can play different roles in a program :Inside the function – local variables
Local variables : Those variable, which are declared within the function are called local variables because scope of these type of declaration of variables only exist ot the function in which these are defined.
int add(void)
{
int x;
int y;
int s;
x = 10;
y = 20 ;
s = x + y ;
return(s);
}
int diff(void)
{
int x;
int y;
int d;
x = 10;
y = 20;
d = y – x;
return(d);
}
In above program, the variable x, y and s are local because scope of these variables are only within the function add(void). Same variables x and y are declared at the start of the function diff(void). The local variables are declared at the start of the function body. These variables can be declared within code block.
Global Variables :The scope of global variables remain in the entire program and these type of variables can be used by any piece of code. These variables hold their values
For example :
int
func (int, int);
void
result (void);
int
i = 40;
int
sum;
void
main ()
{
int sum = 500;
clrscr( );
result( );
}
void
result(void )
{
int
output;
printf(“%d”,
func(i, i);
output
= i * func(i, i);
printf(“%d”,
output);
}
int
func (int x, int y)
{
sum
= x + y;
return
(sum);
0 comments:
Post a Comment