What is variables:-
A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
Types of Variables in C
There are many types of variables in c:
(1) local variable
(2) global variable
(3) static variable
(4) automatic variable
(5) external variable
(1) local variable
Those variables that declared inside a block or function is known as local variable.it can be used only in the block where it is declared.
For Example-
- Void main ()
- {
- int p,q; //local variable
- }
(2) global variable
Those variables that declared in the declaration before the main function block know as global variable.
Global variables can be used throughout the program.
For Example-
- int x,y; //global variable
- void main ()
- {
- ..........................
- }
(3) static variable
A static int variable remains in memory while the program is running. A normal or auto variable is destroyed when a function call where the variable was declared is over. ... 4) In C, static variables can only be initialized using constant literals. For example, following program fails in compilation.
For Example-
- int fun ()
- {
- static int count = 0;
- count ++;
- return count ;
- }
- int main ()
- {
- printf ("%d",fun());
- printf ("%d",fun());
- return 0;
- }
(4) automatic variable
The variables which are declared inside a block are known as automatic or local variables; these variables allocates memory automatically upon entry to that block and free the occupied memory upon exit from that block. Here, both variables a and b are automatic variables.
For Example-
- void ecodepoint ()
- { int x;
- float y;
- char z;
- }
- int main()
- { int a,b;
- ecodepoint ();
- return 0;
- }
(5) external variable
External variables are also known as global variables. These variables are defined outside the function. These variables are available globally throughout the function execution. The value of global variables can be modified by the functions. “extern” keyword is used to declare and define the external variables.
For Example-
- extern int x = 32;
- int b = 8;
- int main()
- { auto int a = 28;
- extern int b;
- printf ("The valu of auto variable : %d\n",a);
- printf ("The valu of extern variable x and b : %d,%d\n",x,b);
- x = 15;
- printf ("The value of modified extern variable x : %d\n",x);
- return 0;
- }
No comments:
Post a Comment