-1

I thought I had understood how to properly increment a value in C until I ran this piece of code. As we see from the output, static_var keeps on getting incremented. That is not the case, however, with var. I would think that var should keep on getting incremented by 1 each time as well.

If I were to change int var to static int var, then var does keep on getting incremented. Maybe I'm not thinking correctly. If someone could clear this up as to "why" var does not keep on getting incremented each, that would be fantastic!

#include <stdio.h>

void function() { 
    int var = 5;
    static int static_var = 5; 
    printf("\t[in function] var = %d\n", var);
    printf("\t[in function] static_var = %d\n", static_var);
    var++; 
    static_var++; 
}

int main() { 
    int i;
    static int static_var = 1337; 
    for(i=0; i < 5; i++) { 
        printf("[in main] static_var = %d\n", static_var);
        function(); 
    }
}

OUTPUT:

[in main] static_var = 1337
[in function] var = 5
[in function] static_var = 5
[in main] static_var = 1337
[in function] var = 5
[in function] static_var = 6
[in main] static_var = 1337
[in function] var = 5
[in function] static_var = 7
[in main] static_var = 1337
[in function] var = 5
[in function] static_var = 8
[in main] static_var = 1337
[in function] var = 5
[in function] static_var = 9
user3754974
  • 279
  • 1
  • 6
  • 15
  • 3
    They are two separate, unrelated variables. What makes you think they should be one and the same? – NPE Aug 16 '14 at 05:56
  • Start by [reading this](http://stackoverflow.com/questions/572547/what-does-static-mean-in-a-c-program). – WhozCraig Aug 16 '14 at 06:04
  • `int var = 5;` means that `var` is set to `5` when the function is entered ... and `var` ceases to exist when the function returns, since it is not static – M.M Aug 16 '14 at 06:18
  • @user3754974: Why do you expect `var` to "keep on getting incremented", when you explicitly create variable `var` with initial value `5` every time you enter the function? Variable `var` is destroyed every time you leave the function and created anew (with `5` in it) every time you enter the function. It is a new variable every time. It simply can't "keep on getting incremented". – AnT stands with Russia Aug 16 '14 at 07:00
  • @AndreyT I would think that `var` would keep on getting incremented because of `var++;` in `function`. I just don't understand why this would not be the case unless I explicitly make `var` a static variable. – user3754974 Aug 16 '14 at 07:16

5 Answers5

2

The problem is scope. When you define a variable var in your function, it creates a new variable for that block that uses the name. The compiler makes all references to var in that function map to your new variable. It works this way because the language assumes that if you created a new variable of the same name with smaller scope, you must have meant it. If you wanted to access the static variable in main and in function, you need to declare it at global scope outside of either function.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
1

The code you wrote is essentially equivalent to this

static int static_var_from_function = 5; 

void function() { 
    int var = 5;
    printf("\t[in function] var = %d\n", var);
    printf("\t[in function] static_var = %d\n", static_var_from_function);
    var++; 
    static_var_from_function++; 
}

Your static_var is equivalent to a file-scope variable ("global variable" if you will), which is created (with initial value of 5) only once when your program starts. That variable lives forever and forever keeps its last value. That's why you see it incremented continuously every time you enter the function.

Meanwhile your var is a local variable that is created anew every time you enter the function (with initial value of 5) and destroyed when the function ends. That is why every time you enter the function it begins incrementing from 5.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

I think you are confused between visibility and duration.

If we declared static variable locally then its visibility will within a block where it has declared and duration is life time of program.

it controls duration, not visibility. Visibility is already decided since it's inside the function - it can't be seen outside the function.

Now your first variable static int static_var = 5; in your function name function which is local to that block.no effect to other block (ie. to main block ).

Your second variable with same name static int static_var = 5; in main block is local to that.No effect on other block (ie. to function body block ).

So in summary both are independent variables with same name. No relation between them.

when you use static keyword

static int var = 5; // Note: This is initialization not assignment.

then this variable initializes only one time in whole program and preserve it's last assigned value.

And when you use non-static variable then

int var = 5;  // every time created when function call and destroyed when function end.
             // so it is not one time initialization, we can initialize it again.
             // It will every time set value to 5 when you enter to function

Key points for static variable :

  1. If we declared static variable locally then its visibility will within a block where it has declared.

  2. If declared a static variable or function globally then its visibility will only the file in which it has declared not in the other files

Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
0

The initialization of the non-static int var = 5; occurs each time function() is called.

"If an initialization is specified for the object, it is performed each time the declaration or compound literal is reached in the execution of the block; ..." C11dr §6.2.4

The initialization of static int static_var = 5; occurs once; before main() even begins.

"All objects with static storage duration shall be initialized (set to their initial values) before program startup" C11dr §5.1.2 1

void function() { 
    int var = 5;                // Assigned to 5 each function execution
    static int static_var = 5;  // Assigned to 5 once, on program start.
    printf("\t[in function] var = %d\n", var);
    printf("\t[in function] static_var = %d\n", static_var);
    var++; 
    static_var++; 
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
-2

So, you have three variables - static int in main part and both int and static int in your function. The point is static variable lives in scope, where it was declared. The usual int creates (and initializes) every time, when runing program reaches scope, when it was declared.
Thats why every time func was called - "usual" would be the same, whilst static variable "remembers" its value (it acts like a global variable).

ars
  • 707
  • 4
  • 14
  • The author didn't asked about these two variables at all. There also is no reason to assume, he is confused by the fact that variable in main() didn't changed. So, you didn't answer the question. – Alexey Shmalko Aug 16 '14 at 06:10
  • @Alexey Shmalko, i see, that i incorrectly understand the initial question. Ipdating my answer. Thank your for pointing that. – ars Aug 16 '14 at 07:04