In a program consisting of a number of functions a number of different types of variables can be found
Global vs static variable-Global variables are recognized through out the program whereas local variables are recognized only inside the function.
Static vs Dynamic variables- Retention of value by a local variable means that in ,static retention of the variable value Is lost once the function is completely executed whereas in certain conditions the value of the variable has to be retained from the earlier execution and the execution retained .
The Variables can be characterized by their data type and their storage classes .One way to classify a variable is according to its data type and the other can be through its storage class. Data type refers to the type of value represented by a variable whereas storage class refers to the permanence of a variable and its scope with in the program .
Storage Classes
There are four different storage classes specified in C:
I. Auto (mark)
2. Extern tall
3. Static
4. Register
The storage class associated with a variable can sometimes be established by the location of the variable declaration within the program.
For example - auto int a, b;
static int a,b;
Automatic Variables
The variables local to a function are automatic i.e.. declared within the function The scope of lies within the function itself. The automatic defined in different functions,even if they have same name, are treated as different .It is the default storage class f. variables declared in a function.
• The auto is optional therefore there is no need to write it.
• All the formal arguments also have the auto storage class.
• The initialization of the auto-variables can be done:
• in declarations
• using assignment expression in a function
• If not initialized the unpredictable value is defined.
• The value is not retained after exit from the program.
EXAMPLE
#include<stdio.h>
main(int argc,char*argv[])
{
int a,b ;
double d;
printf("%d",argc);
a=10;
b=5;
d=(b*b)-(a/2);
printf("%d,%d%f,a,b,d");
}
All variable a,b,d argc and argv[] have automatic storage classes.
External (Global) Variables
These are not confined to a single function. Their scope ranges from the point of declaration to the entire remaining program. Therefore, their scope,may be the entire program or more functions depending upon where they are declared.
•These are global and can be accessed by any function within its scope.Therefore value may be assigned in one and can be written in another.
• There is difference in external variable definition and declaration.
• External Definition is the same as any variable declaration:
• Usually lies outside or before the function accessing it.
• It allocates storage space required .
•Initial value can be assigned.
•The external specifier is not required in external variable definition.
• A declaration is required if the external variable definition comes after the function definition.
•A declaration begins with an external specifier.
• Only when external variable is defined is the storage spate allocated.
• External variables can be assigned initial values as a part of variable definitions, but the values must be constants rather than expressions.
• If initial value is not included then it is automatically assigned a value of zero.
EXAMPLE
/*Program to illustrate the use of global variables.*/
# include <stdio.h>
int gv; // gv is a global variable
main(){ void function ();
gv= 10; 1
printf ("%d is the value of gv before function call\n", gv);
function I( );
print(" %d is the value of gv after function call\n", gv);
}
void function()
{
gv=15;
}
OUTPUT
10 is the value gv before function calling
15 is the value gv after function calling
Static Variables
In case of single file program static variables are defined within functions and individually have the same scope as automatic variables. But static variables retain their values throughout the execution of program within their previous values.
• Th. specifier precedes the declaration. Static and the value cannot be accessed outside of their defining function.
• The static variables may have same name as that of external variables but the local variables take precedence in the function. Therefore external variables maintain their independence with locally defined auto and static variables.
•Initial value Is expressed as the constant and not expression.
•Zeros are assigned to all variables whose declarations do not Include explicit initial values. Hence they always have assigned values.
•Initialization Is done only is the first execution.
Example
/* Program to illustrate the use of static variable*/
include<stdio.h>
main()
{
int call-static();
int i,j;
i=j=0;
j=call_static();
printf("%d\n",j);
j=call_static();
printf("%d\n",j);
j=call_static();
printf("%d\n",j);
}
int call_static(){
static int i=1;
int j;
j=i;
i++;
return(j);
}
OUTPUT
1
2
3
Register Variables
Besides three storage class specifications namely, Automatic, External and Static there is a register storage class. Registers are special storage areas with in a computer's CPU. All the arithmetic and logical operators are carried out with these registers.
For the same program, the execution time can be reduced if certain values can be stored in registers rather than memory. These programs are smaller in size and few data transfers are required. The reduction is there machine code and not in source code. They are declared by the proceeding declaration by register reserved word as follows:
register int m;
Global vs static variable-Global variables are recognized through out the program whereas local variables are recognized only inside the function.
Static vs Dynamic variables- Retention of value by a local variable means that in ,static retention of the variable value Is lost once the function is completely executed whereas in certain conditions the value of the variable has to be retained from the earlier execution and the execution retained .
The Variables can be characterized by their data type and their storage classes .One way to classify a variable is according to its data type and the other can be through its storage class. Data type refers to the type of value represented by a variable whereas storage class refers to the permanence of a variable and its scope with in the program .
Storage Classes
There are four different storage classes specified in C:
I. Auto (mark)
2. Extern tall
3. Static
4. Register
The storage class associated with a variable can sometimes be established by the location of the variable declaration within the program.
For example - auto int a, b;
static int a,b;
Automatic Variables
The variables local to a function are automatic i.e.. declared within the function The scope of lies within the function itself. The automatic defined in different functions,even if they have same name, are treated as different .It is the default storage class f. variables declared in a function.
• The auto is optional therefore there is no need to write it.
• All the formal arguments also have the auto storage class.
• The initialization of the auto-variables can be done:
• in declarations
• using assignment expression in a function
• If not initialized the unpredictable value is defined.
• The value is not retained after exit from the program.
EXAMPLE
#include<stdio.h>
main(int argc,char*argv[])
{
int a,b ;
double d;
printf("%d",argc);
a=10;
b=5;
d=(b*b)-(a/2);
printf("%d,%d%f,a,b,d");
}
All variable a,b,d argc and argv[] have automatic storage classes.
External (Global) Variables
These are not confined to a single function. Their scope ranges from the point of declaration to the entire remaining program. Therefore, their scope,may be the entire program or more functions depending upon where they are declared.
•These are global and can be accessed by any function within its scope.Therefore value may be assigned in one and can be written in another.
• There is difference in external variable definition and declaration.
• External Definition is the same as any variable declaration:
• Usually lies outside or before the function accessing it.
• It allocates storage space required .
•Initial value can be assigned.
•The external specifier is not required in external variable definition.
• A declaration is required if the external variable definition comes after the function definition.
•A declaration begins with an external specifier.
• Only when external variable is defined is the storage spate allocated.
• External variables can be assigned initial values as a part of variable definitions, but the values must be constants rather than expressions.
• If initial value is not included then it is automatically assigned a value of zero.
EXAMPLE
/*Program to illustrate the use of global variables.*/
# include <stdio.h>
int gv; // gv is a global variable
main(){ void function ();
gv= 10; 1
printf ("%d is the value of gv before function call\n", gv);
function I( );
print(" %d is the value of gv after function call\n", gv);
}
void function()
{
gv=15;
}
OUTPUT
10 is the value gv before function calling
15 is the value gv after function calling
Static Variables
In case of single file program static variables are defined within functions and individually have the same scope as automatic variables. But static variables retain their values throughout the execution of program within their previous values.
• Th. specifier precedes the declaration. Static and the value cannot be accessed outside of their defining function.
• The static variables may have same name as that of external variables but the local variables take precedence in the function. Therefore external variables maintain their independence with locally defined auto and static variables.
•Initial value Is expressed as the constant and not expression.
•Zeros are assigned to all variables whose declarations do not Include explicit initial values. Hence they always have assigned values.
•Initialization Is done only is the first execution.
Example
/* Program to illustrate the use of static variable*/
include<stdio.h>
main()
{
int call-static();
int i,j;
i=j=0;
j=call_static();
printf("%d\n",j);
j=call_static();
printf("%d\n",j);
j=call_static();
printf("%d\n",j);
}
int call_static(){
static int i=1;
int j;
j=i;
i++;
return(j);
}
OUTPUT
1
2
3
Register Variables
Besides three storage class specifications namely, Automatic, External and Static there is a register storage class. Registers are special storage areas with in a computer's CPU. All the arithmetic and logical operators are carried out with these registers.
For the same program, the execution time can be reduced if certain values can be stored in registers rather than memory. These programs are smaller in size and few data transfers are required. The reduction is there machine code and not in source code. They are declared by the proceeding declaration by register reserved word as follows:
register int m;
- These variables are stored in registers of computers. If the registers are not available they are put in memory.
- Usually 2 or 3 register variables are there in the program.
- Scope is same as automatic variable, local to a function in which they are declared.
- Address operator '&' cannot be applied to a register variable.
- If the register is not available the variable is though to be like the automatic variable.
- Usually associated integer variable but with other types it is allowed having same size (short or unsigned).
- Can be formal arguments in functions.
- Pointers to register variables are not allowed.
- These variables can be used for loop indices also to increase efficiency.
0 comments:
Post a Comment