Thread: How static variable useful in c

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    40

    How static variable useful in c

    I do not understand how the static variable is useful in c language

    I wrote my program for global and local static. When I run both program They both give the same result

    Global static variable
    Code:
    #include <stdio.h>
    
     static int a = 0; 
       
    int main() 
    { 
      a =  5;
      
      printf("%d ", a); 
    
    
        return 0;
    }
    
    5
    Local static variable

    Code:
    #include <stdio.h>
    
    
    int main() 
    { 
        static int a = 0; 
      
        a =  5;
      
      printf("%d ", a); 
    
      return 0;
    }
    5

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Both have static storage duration, and typically what makes a static local useful (that it persists between function calls and you can return a pointer to it despite it being local) also means it becomes part of global state, and hence like global variables can make it harder to reason about your program, especially in the face of concurrency and parallelism. The difference is a matter of scope: you need to be a little more deliberate to change the static local from outside its scope (and you might only be able to do so indirectly), whereas a global variable could be directly changed from anywhere.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    40
    Quote Originally Posted by laserlight View Post
    Both have static storage duration, and typically what makes a static local useful
    Local static
    Code:
    #include<stdio.h> int fun() 
    { 
      static int a = 0; 
       a = a + 1; 
      return a; 
    } 
       
    int main() 
    { 
      
      printf("%d ", fun()); 
      printf("%d ", fun()); 
      printf("%d ", fun()); 
      printf("%d ", fun());  
      
      return 0; 
    }
    Global static

    Code:
    #include<stdio.h> 
    
    static int a = 0;
    
    
    int fun() 
    { 
       
       a = a + 1; 
      return a; 
    } 
       
    int main() 
    { 
      
      printf("%d ", fun()); 
      printf("%d ", fun()); 
      printf("%d ", fun()); 
      printf("%d ", fun());  
      
      return 0; 
    }
    Both are the codes gives same output

    I do not understand when do we use static global and local static in code

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by skyr6546
    I do not understand when do we use static global and local static in code
    Extern global: use them when you need global state that is really meant to be accessed directly throughout your program. An example of this that you have encountered would be stdin and stdout... although even then you only modify the global state they represent through functions.

    Static global: use them when you have global state whose direct access you want to try and limit to file scope. Usually this means some kind of implementation detail state that needs to be accessed throughout a particular library component.

    Static local: use them when you want state originating from a function to persist across function calls. An example of this would be strtok from <string.h>, in which a plausible implementation might use a static local pointer to point to the token. An implementation could use a static global instead, but there's no point increasing the scope of the variable when it is relevant to only one function.

    That said, until you have become more proficient in C, try not to use any of them, i.e., prefer to use non-static local variables, to the extent that you force yourself to stick to using non-static local variables in as small a scope as feasible.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static Variable and Static method
    By codewriter in forum C++ Programming
    Replies: 6
    Last Post: 03-25-2012, 07:49 AM
  2. Replies: 8
    Last Post: 09-27-2010, 04:11 PM
  3. Replies: 8
    Last Post: 01-19-2009, 07:42 PM
  4. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  5. Replies: 6
    Last Post: 12-13-2007, 08:20 PM

Tags for this Thread