Thread: Static storage class

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    Static storage class

    What happen when we use static storage class in front of variable name

    If I use static or if I don't use static in this program, nothing change

    What the benefits of static storage class in program

    Code:
    #include<stdio.h>
    
    void loop (void)
    {
    	 static int count;
    	for (count = 0; count < 5; count++)
    	{
    		printf("Number :  %d \n", count);
    	}
    }
    int main(void)
    {  	
        loop();
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Try this:

    Code:
    #include<stdio.h>
     
    void loop (void)
    {
         static int count = 0;
         printf("Number :  %d \n", count++);
    }
    
    int main(void)
    {   
        for(int i = 0; i < 5; ++i)
            loop();
     
     
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by jimblumberg View Post
    Try this:

    Code:
    int main(void)
    {   
        for(int i = 0; i < 5; ++i)
            loop();
     
    }
    I didn't get your point What is the meaning of those lines why you are incrementing counter in loop function

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    The point is for YOU to experiment with calling the same code, where the only difference is the use of the static keyword.

    In the hope you'll learn something.

    TBH, your simple questions have been going on so long now, you're bordering on being a troll.
    Seriously, you need to pick up the pace of learning if you ever hope to write a meaningful program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Salem View Post
    The point is for YOU to experiment with calling the same code, where the only difference is the use of the static keyword.

    In the hope you'll learn something.

    TBH, your simple questions have been going on so long now, you're bordering on being a troll.
    Seriously, you need to pick up the pace of learning if you ever hope to write a meaningful program.
    Help Vampire is a closer match.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by Salem View Post
    The point is for YOU to experiment with calling the same code, where the only difference is the use of the static keyword. In the hope you'll learn something..
    I did the two experiment. I printed value with static and without static

    Program with static
    Code:
    #include<stdio.h>  
    void loop (void)
    {
         static int count = 0;
         printf("Number :  %d \n", count++);
    }
     
    int main(void)
    {   
        for(int i = 0; i < 5; ++i)
            loop();
        
        return 0;
    }
    Number : 0
    Number : 1
    Number : 2
    Number : 3
    Number : 4

    If I write static before variable , so every call of the function number would change.

    If I don't write static before variable , so every call of the function number would be 0
    Code:
    #include<stdio.h>
      
    void loop (void)
    {
         int count = 0;
        printf("Number :  %d \n", count++);
    }
     
    int main(void)
    {   
        for(int i = 0; i < 5; ++i)
            loop();
        
        return 0;
    }
    Number : 0
    Number : 0
    Number : 0
    Number : 0
    Number : 0


    Quote Originally Posted by Salem View Post
    Seriously, you need to pick up the pace of learning if you ever hope to write a meaningful program.
    I wrote this program
    Code:
    #include <stdio.h>
    
    
    void loop(void)
    {
        int a = 0;
        static int static_a = 0;
    
    
        a += 5;
        static_a += 5;
    
    
        printf(" \n a = %d, static_a = %d", a, static_a);
    }
    
    
    
    
    int main(void)
    {
        int i;
    
    
        for (i = 0; i < 5; ++i)
        loop();
    	return 0;
    }
    a = 5, static_a = 5
    a = 5, static_a = 10
    a = 5, static_a = 15
    a = 5, static_a = 20
    a = 5, static_a = 25




    Quote Originally Posted by Salem View Post
    you're bordering on being a troll..
    Why do you think that? I do my best to solve problrm

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by stahta01 View Post
    Help Vampire is a closer match.

    Tim S.
    Help Vampire | Community Management Wiki | FANDOM powered by Wikia



    Note: Not all symptoms listed; just the ones I see in a "Help Vampire".
    Edit: It might just be the ones I really dislike seeing in a poster.
    Typical symptoms include:
    Inability to use web search (eg Google) effectively.
    Inability to "try it and see", when asking programming questions.
    No apparent increase in comprehension after questions are answered.
    Tim S.
    Last edited by stahta01; 02-20-2018 at 10:25 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I did the two experiment. I printed value with static and without static
    And what did those experiments tell you? Did they answer your original question?

    I wrote this program
    Okay, and what did that program prove/disprove?

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by jimblumberg View Post
    And what did those experiments tell you? Did they answer your original question?

    Okay, and what did that program prove/disprove?
    I understood the difference. I can see in experiment. If I don't use the static before variable then, every function call, the value of variable doesn't change. if the I use the static before variable name, every function call value in variable change

  10. #10
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by vead View Post
    I understood the difference. I can see in experiment. If I don't use the static before variable then, every function call, the value of variable doesn't change. if the I use the static before variable name, every function call value in variable change

    a) What are the advantages and disadvantages of the static version?
    b) Can you think of any times you might use static?

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    c) Can you think of any times you should avoid the use of static?

    And these experiments are only dealing with a static variable inside a function.

    d) What happens when you use static outside of any function.
    e) What happens when you use static with a function it's self?

    And lastly you seem to be leaving out a very important aspect of using a static variable inside a function, what is that aspect?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static is invalid storage class
    By Cotant in forum C Programming
    Replies: 2
    Last Post: 10-02-2015, 03:14 PM
  2. Need help: Explanation of a static storage class
    By Sam Pertuit in forum C Programming
    Replies: 7
    Last Post: 01-30-2013, 09:34 AM
  3. Doubt regarding storage of a static variable
    By karthik537 in forum C Programming
    Replies: 6
    Last Post: 02-21-2012, 01:21 PM
  4. Static storage duration question.
    By nimitzhunter in forum C Programming
    Replies: 5
    Last Post: 08-10-2010, 01:18 PM
  5. static storage class
    By roaan in forum C Programming
    Replies: 4
    Last Post: 09-08-2009, 04:57 PM

Tags for this Thread