Static variable inside of a function in C

static variables in c
static global variable in c
static function in c
static variable in c++
difference between static local and static global variable in c
static variable vs global variable
static array in c
static variable inside function java

What will be printed out? 6 6 or 6 7? And why?

void foo()
{
    static int x = 5;
    x++;
    printf("%d", x);
}

int main()
{
    foo();
    foo();
    return 0;
}

There are two issues here, lifetime and scope.

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Static Variables in C, Static local variables: variables declared as static inside a function are statically allocated while having the same scope as automatic local variables. Hence whatever values the function puts into its static local variables during one call will still be present when the function is called again. Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope. Syntax: static data_type var_name = var_value; Following are some interesting facts about static variables in C. 1) A static int variable remains in memory while the program is running. A normal or auto variable is destroyed when a function call where the variable was declared is over.

Output: 6 7

Reason: static variable is initialised only once (unlike auto variable) and further definition of static variable would be bypassed during runtime. And if it is not initialised manually, it is initialised by value 0 automatically. So,

void foo() {
    static int x = 5; // assigns value of 5 only once
    x++;
    printf("%d", x);
}

int main() {
    foo(); // x = 6
    foo(); // x = 7
    return 0;
}

Static variable inside of a function in C, First of all, let's understand precisely where a local variable generally lives in a C or C++ program (or most other languages) by default… This means that it is an  A static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name. An example that demonstrates this is given as follows:

That is the same as having the following program:

static int x = 5;

void foo()
{
    x++;
    printf("%d", x);
}

int main()
{
     foo();
     foo();
     return 0;
}

All that the static keyword does in that program is it tells the compiler (essentially) 'hey, I have a variable here that I don't want anyone else accessing, don't tell anyone else it exists'.

Inside a method, the static keyword tells the compiler the same as above, but also, 'don't tell anyone that this exists outside of this function, it should only be accessible inside this function'.

I hope this helps

Where does a static variable inside a function in C/C++ lives?, in the statically allocated memory instead of the automatically allocated memory. Static variables are declared with static keyword. Static variables have an essential property of preserving its value across various function calls. Unlike local variables, static variables are not allocated on C stack. Rather, they get their memory in data segment of the program.

6 7

compiler arranges that static variable initialization does not happen each time the function is entered

what is the use of static variable in c?, in the uninitialized data segment( also known as the BSS segment). Storage Classes of C, extern, register, static are the four storage classes in 'C'. auto is used for a local variable defined within a block or function.

A static variable inside a function has a lifespan as long as your program runs. It won't be allocated every time your function is called and deallocated when your function returns.

Static variable, The variables which are declared inside the function, compound statement (or block) are called Local variables. 1. 2. 3. 4. 5. 6. 7. Now, look at the main() function, here we created an integer pointer p that is storing the address returned by function fun(), after every function call, static variable increased, because the default value of the static variable is 0 and then its lifetime is in the whole program.

What does "static" mean in C?, The compiler persists with the variable till the end of the program. Static variables can be defined inside or outside the function. They are local  The Static variables are come into play once the program execution starts and it remain available till the program execution ends. The Static variables are created in the Data Segment of the Memory.

Where are static variables stored in C/C++?, Static Keyword in C++ Static Variables inside Functions. Static variables when used inside function are initialized only once, and then they hold there value  You must realise that the static variable inside the function, simply put, a global variable hidden to all but the function's scope, meaning that only the function it is declared inside can reach it. Inlining the function won't change anything: inline void doSomething() { static int value ; } There will be only one hidden global variable.

Local, Global and Static variables in C, Inside a function, you can initialize a static variable using the static keyword. I said “inside a function”, because global variables are static by  Static Variables inside Functions. Static variables when used inside function are initialized only once, and then they hold there value even through function calls. These static variables are stored on static storage area , not in stack. void counter() { static int count=0; cout << count++; } int main(0 { for(int i=0;i<5;i++) { counter(); } }

Comments
  • What's the problem to try?
  • Did you try to type this in and see for yourself?
  • I want to understand why.
  • @Vadiklk so ask question starting with "Why"
  • ideone.com/t9Bbe What would you expect? Does the result not match your expection? Why did you expect your result?
  • @devanl, yes we are.
  • Simple and logical :)
  • in what scenarios we need to declare a variable as static inside a function?, just curious to know as I haven't used this before?
  • i'd say thanks, but this was all answered at the very tip top of the page. it makes me laugh that people don't just run their own code. xD
  • This answer is wrong. The moment you think about recursive functions the definitions as described here do not explain the behavior!
  • Well, it's not actually the same. There's still the issue of scope on X. In this example, you could poke and futz with x in main; it is global. In the original example x was local to foo, only visible while inside that block, which is generally preferable: if foo exists to maintain x in predictable and visible ways, then letting others poke it is generally dangerous. As another benefit of keeping it in scope foo() It also keeps foo() portable.
  • @user2149140 'don't tell anyone that this exists outside of this function, it should only be accessible inside this function'
  • While you've addressed the issue of scope due to where the variable is declared, the description of static as affecting scope, rather than lifetime, seems incorrect.
  • @Chameleon The question is tagged as c, so in this context, your example would be illegal at global scope. (C requires constant initializers for globals, C++ does not).