Types of variable in C

In c language a variable categorized in following two types these are:-
(a) Local variable
(b) Global variable

NOTE:- when it is require a memory space then compiler request to operating system for memory space where operating system internally divide RAM into four region.

   (1)    Local variable- A variable define within open({) and close(}) curly braces are known as Local variable and it poses/ hold following properties:-
(a)    A local variable allocates in satck region.
(b)   A local variable by default intialise by garbage.
(c)    A local variable can be access by only those statement which are enclosed in its own open and closed curly braces.
(d)   A local variable automatically deallocates when it’s closed curly braces is found.

{
datatype varname[=value],……….;
……………..;
}
Syntax:-



                                                                                                                                               
Example:-
   (a)    void main()
{
        int a;
         clrscr();
         {
int b=20,c;
printf(“value of a=%d”,a);
printf(“\n b=%d,c=%d”,b,c);
}
         printf(“\n value of a=%d”,a);
                        printf(“\n value of b=%d”,b);
getch();
}
Output-------àvalue of a=10
                                value of b=20,c=85
                                value of a=10

Note:- A garbage value may be predicted.
Example:-
         void main()
{
int a=5;
clrscr();
{
         int b=10;
         printf(“a=%d,b=%d”,a,b);
}
{
int c;
printf(“\n value of c=%d”,c);
}
getch();
}
Output---à a=5,b=10
                         value of c=10
·         In stack region memory space deallocated as LIFO (Last In First Out) operation.
Example:-
         void main()
{
         int a=5,b=10;
         printf(“a=%d,b=%d”,a,b);
}
{
int c;
printf(“\n c=%d”,c);
}
getch();
}
Output----------à a=5,b=10
                                c=5

STACK
   (a)    Global variable:- A variable define outside of open({) and close(}) curly braces are known as global variable and it poses/holds following properties:-
(i)                  A global variable allocates into global region.
(ii)                A global variable by default initialize by 0.
(iii)               A global variable can be access by all followed statement.
(iv)              A global variable automatic deallocates when program terminate.
Explanation:-
     (1)                              int a=10,b; //global variable
                                          void disp()
                                          {
                                          b=20;
                                          printf(“\n a=%d,b=%d”,a,b);
                                          }
                                          void main()
                                          {
                                          clrscr();
                                          {
                                          int c=50;
                                          printf(“a=%d,b=%d,c=%d”,a,b,c);
                                          }
                                          printf(“\n a=%d,b=%d”,a,b);
                                          disp();
                                          getch();
                                          }
Output--------------à a=10,b=0,c=50
                                          a=10,b=0
                                          a=10,b=20
      (2)    void disp()
{
printf(“\ndisp() called!”);
}
int a=10;      //global variable
void main()
{
clrscr();
printf(“value of a =%d”,a);
disp();
getch();
}
Output-------à           value of a=10
                                          disp() called!
NOTE:- In C language more than one variable can have same name.
Example:-
                                          int a=10;
                                          void main()
{
                                          int a=20;
                                          clrscr();
                                          {
                                                int a=30;
                                                printf(“Value of a=%d”,a);
                                          }
                                          printf(“\n Value of a=%d”,a);
                                          printf(“\nValue of a=%d”,::a);    //“::” is scope resolution operator or access operator
//
                                          getch();
}
Output------------à Value of a=30
                                          Value of a=20
                                          Value of a=10
NOTE:-  When we define global variable and local variable as a same name then global variable hides by local variable and this process is known local variable and this process is known as shadowing.
                                          In this case if we want to access global variable then a scope resolution operator(::) must be used with global variable as a following format:

var=::gvar;


Syntax:-


printf(“<message prompt><format specifier>”,::gvar);
                          

Comments

Popular Posts