Thread: static variables inside a function.

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    91

    static variables inside a function.

    please see the following code:
    Code:
    #include<stdio.h>
    
    void f()
    {
    static int a=0;
    a=a+3;
    printf("%d",a);
    }
    
    void main()
    {
    
    f();
    f();
    }
    here the output is 3 6.

    so my question is:

    when the function f is called the second time,does the compiler ignore the declaration of a inside the function?
    please explain to me what happens here.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You've declared a as static... so it will persist between function calls.

    first time... no a so it creates it,
    second time... a exists so it uses the existing one.

    And, fwiw... calling a function twice is not recursion. Recursion happens when a function calls itself.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    ok got it..sorry that was a typo its not recursion.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    when the function f is called the second time,does the compiler ignore the declaration of a inside the function?
    A more interesting question is will the compiler ignore the initialization on subsequent calls. And the answer is yes, provided you truly initialize rather than simply assign:
    Code:
    void foo()
    {
        static int a = 0; /* Initialization happens once */
        static int b;
    
        b = 0; /* "Initialization" happens every call */
    
        ....
    }
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    that's really useful information......so if i were to separate the assignment and initialization,then a will be assigned 0 and the o/p
    would differ.

    really good post for c rookies..

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by theju112 View Post
    really good post for c rookies..
    Rookies? I've been at this for 6 years now and Prelude makes me look like a beginner!

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by CommonTater View Post
    Rookies? I've been at this for 6 years now and Prelude makes me look like a beginner!
    You can't compete with a goddess tater.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    so if i were to separate the assignment and initialization,then a will be assigned 0 and the o/p
    would differ.
    Here's a more complete example that shows the difference:
    Code:
    #include <stdio.h>
    
    void foo()
    {
        static int a = 0; /* Initialization happens once */
        static int b;
    
        b = 0; /* "Initialization" happens every call */
    
        printf("a=%d\tb=%d\n", a++, b++);
    }
    
    int main(void)
    {
        foo();
        foo();
        foo();
    
        return 0;
    }
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by Prelude View Post
    Code:
    printf("a=%d\tb=%d\n", a++, b++);
    Now that we're on the topic of rookies: very very bad coding practice to show a beginner!
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    very very bad coding practice to show a beginner!
    I'm not in the business of dumbing down my code to protect beginners from reality. They're going to see far worse expressions, so better to get things moving as quickly as possible. Though since you seem so adamant about it, why not offer up an explanation for the beginner's reading your post?
    My best code is written with the delete key.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Dunno, it seems fine to me since the expressions are standalone and it is clear that the variables involved are not repeated or aliases. It feels akin to writing:
    Code:
    array[size++] = element;
    which I would posit is not bad coding practice.
    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

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    Dunno, it seems fine to me since the expressions are standalone and it is clear that the variables involved are not repeated or aliases. It feels akin to writing:
    Code:
    array[size++] = element;
    which I would posit is not bad coding practice.
    I would agree that it is not bad. However, it also runs afoul of coding guidelines that require any non-declarative statement to have only one observable effect other than an overt function call. That statement has two observable effects: it changes the value of array[size] and increments size.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by MWAAAHAAA View Post
    Now that we're on the topic of rookies: very very bad coding practice to show a beginner!
    Why? It's perfectly legal and invokes no undefined behavior. If they don't know what the increment operator is, it's as good a time as any to go look it up.
    Quote Originally Posted by grumpy View Post
    However, it also runs afoul of coding guidelines that require any non-declarative statement to have only one observable effect other than an overt function call. That statement has two observable effects: it changes the value of array[size] and increments size.
    Whose guidelines are those?


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    all you guys go and post answers in other threads..my doubt regarding static variables are clear.
    thank you all for your elderly love.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static variable inside member function?
    By Memloop in forum C++ Programming
    Replies: 4
    Last Post: 11-26-2009, 03:06 PM
  2. variables when declared inside or outside a function
    By jas_atwal in forum C Programming
    Replies: 6
    Last Post: 12-14-2007, 02:42 PM
  3. Replies: 6
    Last Post: 12-13-2007, 08:20 PM
  4. static pointer in a recursive function?
    By rools in forum C Programming
    Replies: 2
    Last Post: 09-19-2005, 03:38 AM
  5. static variables inside of a class.. possible?
    By revelation437 in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2003, 05:21 PM