Monday, April 6, 2020

VARIABLES IN C

VARIABLES IN C

C-VARIABLES

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
     In definition of the functions – formal parameter
    Outside all functions – global variable
    Actual parameter – defined during invocation of function.

For example, with the name A you can associate , say a number 20, so that whenever the number A is called /used, you get the value 20.   

Local variablesThose 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.

For example :
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);
}

Previous :- Operators in C


Vibhor Sharma

Author & Editor

###

0 comments:

Post a Comment