Distinguish between the following:
(i) Automatic and static variables
(ii) Global and local variables.
Ans:
(i) Automatic and Static variables:
Automatic variables: The features are as follows
Declaration place:-declared inside a function in which they are to be utilized,
that’s why referred as local or internal variables.
Declaration syntax:- A variable declared inside a function without storage class
specification by default is an automatic variable. However, we may use the keyword
auto to declare it explicitly.
main()
auto int age;
}
Default initial value:- Garbage value
Scope:-created when the function is called and destroyed on exit from the function.
Life:- till the control remains within the block in which defined.
Static variables: The features are as follows
Declaration place:-may be declared internally or externally.
Declaration syntax:-we use the keyword static to declare a static variable.
Static int age;
Default initial value:- Zero
Scope:-in case of internal static variable, the scope is local to the function in which
defined while scope of external static variable is to all the functions defined in the
program.
Life:- value of variable persists between different function calls.
(ii) Global and Local Variables
Global variables: The features are as follows
Declared outside of all functions or before main.
These can be used in all the functions in the program.
It need not be declared in other functions.
A global variable is also known as an external variable.
Local variables: The features are as follows
Declared inside a function where it is to be used.
These are not known to other function in the program.
These variables are visible and meaningful inside the functions in which they are
declared.
For example:
#include<stdio.h>
int m;
void main( )
{ int i;
…..
…..
Fun1();
}
Fun1( )
{ int j;
….
…..
}
Here m is a global variable , i is local variable local to main( ) and j is local variable
local to Fun1( ).
No comments:
Post a Comment