The greatest mistake you can make in life is to be continually fearing you will make one

Sunday 12 December 2010

Static Variables in C

  • Static variable use the keyword static.
  • In a single file program static variable is similar to auto variables.
  • Like auto variables, static variables are also local to the block in which they are declared.It cannot be accessed outside of their defining function.
  • The difference between auto and static variable is that static variables dont disappear when the function is no longer active. their values persist.
  • If the control comes back to the same function again the static variables have the same values they had last time.
  • Example
    #include<stdio.h>
    void fun1();
    
    int main()
    {
        fun1();
        fun1();
        fun1();
        return;
    }
    
    void fun1()
    {
        int x=1;
        printf("X=%d\n",x);
        x=x+1;    
    }
    The output of the above program is ./a.out
    X=1
    X=1
    X=1
    If the variable is static then
    #include<stdio.h>
    void fun1();
    
    int main()
    {
        fun1();
        fun1();
        fun1();
        return;
    }
    
    void fun1()
    {
        static int x=1;
        printf("X=%d\n",x);
        x=x+1;    
    }
    The output of the above program is ./a.out
    X=1
    X=2
    X=3
  • If the automatic or static variables having the same names as external variables then the local variables will take precedence over the external variable and the changes done to the local variable will not affect the external variable.
    #include<stdio.h>void fun1();
    int x=1,y=2,z=3;
    int main()
    {
        static float x=5.0;
    
        printf("float x=%f\n",x);
        x=x+1;
        printf("float x=%f\n",x);
        fun1();
        printf("x=%d\n",x); //try to print external var
        printf("x=%f y=%d z=%d\n",x,y,z);    
        return;
    }
    void fun1()
    {
        char z;
        z='A';
        printf("int z=%d char z=%c\n",z,z);
        x=x+10;
        printf("x=%d\n",x);    
    }
    Output
    float x=5.000000
    float x=6.000000
    int z=65 char z=A
    x=11
    x=-1  //This will show an warning
    x=6.000000 y=2 z=3
    Here x,y,z are external integer variable. But x is redefined as a static floating point variable within main. so in main the external variable x will not be recognized. In function fun1() ,z is redefined as a char variable so the external variable z will not be recognized in fun1().
    Example 2
    #include< stdio.h>
    int x=1;
    int main()
    {
        static int x=5;
        printf("x=%d\n",x);
        x=x+1;
        printf("x=%d\n",x);
        return;
    }
    Output:
    x=5
    x=6
Initializing Static Variable:
  • Initial values can be included in the static variable declaration. 
  • Example: static int x=10;
  • The initial values must be constants not expressions.
    Example
    #include<stdio.h>
    int main()
    {
        static int x=5,y=10;
        static int a=x;
        static int z=x+y;
        printf("a=%d\n",a);
        printf("z=%d\n",z);
       return;
    }

    While compiling it shows error as " initializer is not a constant"

  • The initial values are assigned at the beginning of program execution. The variables retain these values throughout the life of the program unless different values are assigned.
  • The default initial value is zero.

No comments:

Post a Comment