How does the lifespan of static variables work?

Asked

Viewed 615 times

14

I just saw a question about C. In it the following code was shown:

#include <stdio.h>

int main() {
    static int a = 5;
    printf("%d", a--);
    if(a)
        main();    
    return 0;
}

Example running on repl.it.

And the challenge was to answer what behavior this code would have (compiled using GCC).

On the face I thought: "infinite loop". Then I went to test and saw that it is not that, the variable a is not assigned to 5 where the method main is called. In fact, the execution shows on the screen 54321 and is finalized.

What is the reason for this behavior?

1 answer

15


First understand difference between scope and lifetime (the original question spoke in scope).

There are two types of static variables in C, one is the local scope (for example) and the other with scope of the code file where it is accessed by any function of the current file, almost as if it were a class member static variable, but it is not even a class.

The lifetime of both is the same as the lifetime of the application, after all the variable is static. This does not change from C#, for example, the difference is that C# does not yet have a local static variable.

So the only difference in this variable is that its visibility is more restricted, it can only be accessed in the function that was declared. Her condition is global, her visibility is local. Therefore it retains the value and each execution will access its global value, it is not lost at the end of the execution of the function.

The global memory area of this variable will only be initialized if the function is executed. Initialization will occur only once.

In the comments it was talked about "intuitiveness". There are a lot of things that are only intuitive when you’ve learned something before, because without prior knowledge you don’t even know where to start. On the other hand, when a person learns something if he sees something other than that, he will tend to think it is not intuitive. If you think about the syntax indicates that well, the variable is within the function and is local, very intuitive, has a word saying that it is static, so it has life time throughout the application, very intuitive, as long as you understand the concept of scope and life time. If you don’t understand this, other ways can get messy too.

Related: Variable Static and #define.

  • Honestly, it is a choice of language design counterintuitive.

  • 1

    Just as JS has hoisting that I find an abomination...

  • 2

    I don’t know, as I learned C right off the bat almost 35 years ago I find it quite normal, of course one can abuse, but I find it quite intuitive, you have the overall state that may be needed and local visibility that lessens the damage it can do. Most language characteristics are not very intuitive, one has to learn first, then one learns and finds it intuitive. In general what one thinks is not intuitive is what goes against what he learned first, I find a lot of "modern" versus intuitive :)

  • It failed to say that the static variables declared within the scope of functions will only be initialized if there is the call of this function. Otherwise, it will never be initialized.

  • @Lacobus is true and is already said by you, although it makes no fundamental difference since it can only be accessed within the function itself. Of course, if you access the address where it is by other means than what the compiler allows and the function has not been called, you will access something not initialized.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.