# difference between static int and int

**URL:** <https://forum.arduino.cc/t/difference-between-static-int-and-int/166631>\
**Category:** Programming\
**Created:** [June 6, 2013, 9:45am UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631 "2013-06-06T09:45:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 6, 2013, 9:45am UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631/1 "2013-06-06T09:45:51Z")

</div>

I am little bit confused in the follwoing program.

```cpp
void myfunction(){  
int my_stack=0;
static int my_static=0;

Serial.print("my_static=");
Serial.println(my_static);

Serial.print("stack=");
Serial.println(my_stack);
my_stack++;
my_static++;
}

```

my\_static variable, declared as static within a function, retains its value between successive calls to my\_function() while my\_stack does not. The output of 5 successive calls to my\_function() is

```cpp
my_static=0
stack=0
my_static=1
stack=0
my_static=2
stack=0
my_static=3
stack=0
my_static=4
stack=0

```

every time when myfunction() runs it comiles static int my\_static=0; then how my\_static can be increased?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 6, 2013, 10:11am UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631/2 "2013-06-06T10:11:06Z")

</div>

> every time when myfunction() runs it comiles static int my\_static=0; then how my\_static can be increased?

No. When the code runs, nothing is compiled.

When the code is compiled, the compiler notices the static keyword in front of my\_static and generates code to initialize the variable ONCE. The compiler notices the lack of the static keyword in front of stack, and generates code to initialize the variable every time.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 6, 2013, 10:38am UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631/3 "2013-06-06T10:38:32Z")

</div>

When you define a local variable as static and equate it to something you are telling it the starting value only. This value only gets assigned once.

It is functionally the same as a global variable, except the scope (where you can access it from) is more limited.

The code to assign the value to the static variable is run during the startup phase (not the setup function - before even that runs) - a process called "[crt0](http://en.wikipedia.org/wiki/Crt0)" for "C RunTime phase 0". This copies all the values for the pre-set (static and global) variables from flash into RAM, and that is the only time these initialization values are ever assigned.

In effect the "=0" of your static declaration is not performed there, but right at the start of the program before even setup() runs.

---

<div class="post-metadata">

**Author:** ![econjack](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/econjack/32/1290082_2.png) [@econjack](https://forum.arduino.cc/u/econjack)\
**Post date:** [June 6, 2013, 1:59pm UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631/4 "2013-06-06T13:59:12Z")

</div>

Your definition of my\_static tells the compiler several things about the variable:

1. Because it is defined within myfunction(), my\_static "lives" and is only visible within the opening and closing braces of myfunction(). That is, my\_static has local scope within myfunction().
2. Because your definition of my\_static is prefaced with the keyword static, it's as though the compiler removes the line of code that defines my\_static out of myfunction() and places it before setup(). This means the definition is only read at program startup. This causes my\_static to get a memory address (i.e., an lvalue) where it will "live" for the entire running of the program and initializes its value (i.e., its rvalue) to 0.

Therefore, when the code enters myfunction() the first time, my\_static becomes visible (it's in scope). When the statement my\_static++ is executed, the compiler fetches the rvalue from memory (i.e., 0) and increments it (rvalue now equals 1). Because you used the static keyword, my\_static is not reallocated each time myfunction() is called. When the second call is performed, the same lvalue is used to fetch the rvalue (which is now 1) and it is again incremented (i.e., rvalue =2). This is why static variables "remember" their previous values. In short, your code is doing exactly what a static variable should do: get allocated once before the program starts and never get reallocated again. Because its lvalue is never reassigned, its rvalue is retained between function calls.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 7, 2013, 4:34am UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631/5 "2013-06-07T04:34:43Z")

</div>

Thanks to all specially econjack ..thanks a lot. Well explanation from every one

---

<div class="post-metadata">

**Author:** ![firashelou](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@firashelou](https://forum.arduino.cc/u/firashelou)\
**Post date:** [September 12, 2015, 3:59pm UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631/6 "2015-09-12T15:59:45Z")

</div>

hello guys,  
i wanted to open a new topic then i saw this one already opened, so i read what is in this post but i couldn't really understand Static!

can you please explain it a very simple way ?

i understood this but not sure:  
static variable should be declared inside a special\_function(){}

then i see some examples declared this static at the beginning of the sketch even before void setup()

and this static if at the starts static = 0

the second time the sketch runs it will be incremented to 1 for example if the program says and it will stay 1 until next run time of the sketch

right ?

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [September 12, 2015, 4:26pm UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631/7 "2015-09-12T16:26:19Z")

</div>

> firashelou:  
> i understood this but not sure:  
> static variable should be declared inside a special\_function(){}

Should? I don't see that anywhere. But you **can**.

---

<div class="post-metadata">

**Author:** ![guix](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/guix/32/215371_2.png) [@guix](https://forum.arduino.cc/u/guix)\
**Post date:** [September 12, 2015, 5:06pm UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631/8 "2015-09-12T17:06:22Z")

</div>

> then i see some examples declared this static at the beginning of the sketch even before void setup()

It's only useful when you make a sketch with multiple files and you want global variables of the same name in more than one file. You add the static keyword to limit this variable to the current file.

Say you have in file1.ino

```arduino
int bla = 1;

void setup()
{
  ...
}

void loop()
{
  ...
}

```

And in file2.cpp you have:

```arduino
int bla = 2;

```

When you compile you will have an error about multiple definitions of variable "bla", because both files are merged (basically). So you change file2.cpp to:

```arduino
static int bla = 2;

```

And there is no more error, because this variable now belong only to file2.cpp.

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [September 12, 2015, 5:25pm UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631/9 "2015-09-12T17:25:51Z")

</div>

Note that pointer magic can do ... well magic .... like changing a static variable inside a function.

( UNO, IDE 1.5.8 )

```cpp
void setup()
{
  Serial.begin(115200);
  Serial.print("Start ");
  Serial.println( __FILE__ );
}

int* f()
{
  static int p = 0;
  Serial.println(p);
  return &p;
}

void g(int *pp)
{
  *pp = *pp + 1;
}

void loop()
{
  int *a = f();
  g(a);
  delay(1000);
}

```

warning: don't do this at home ...

---

<div class="post-metadata">

**Author:** ![firashelou](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@firashelou](https://forum.arduino.cc/u/firashelou)\
**Post date:** [September 12, 2015, 10:53pm UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631/10 "2015-09-12T22:53:36Z")

</div>

> guix:  
> It's only useful when you make a sketch with multiple files and you want global variables of the same name in more than one file. You add the static keyword to limit this variable to the current file.
> 
> Say you have in file1.ino
> 
> ```arduino
> int bla = 1;
> 
> ```

void setup()  
{  
&nbsp; ...  
}

void loop()  
{  
&nbsp; ...  
}

> ```arduino
> 
> And in file2.cpp you have:
> 
> ```
> 
> int bla = 2;
> 
> ```arduino
> 
> When you compile you will have an error about multiple definitions of variable "bla", because both files are merged (basically). So you change file2.cpp to:
> 
> ```
> 
> static int bla = 2;
> 
> ```arduino
> 
> And there is no more error, because this variable now belong only to file2.cpp.
> 
> ```

i don't really get it,  
i know from other languages that static or the world specific for that language means this variable can be used inside one function only, global means any functions can use it

i made a little search i found on a forum that they say a static variable means it can be used in 1 function only the one declared in, but can not be used in another unless it's not static

about your example i don't get it ! why is it .cpp and not .ino ?  
and why would i have multiple files ?! you mean multiple tabs ?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 13, 2015, 2:45am UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631/11 "2015-09-13T02:45:46Z")

</div>

It is functionally the same as a global variable, except the scope (where you can access it from) is more limited.

> majenko:  
> When you define a local variable as static and equate it to something you are telling it the starting value only. This value only gets assigned once.
> 
> It is functionally the same as a global variable, except the scope (where you can access it from) is more limited.
> 
> The code to assign the value to the static variable is run during the startup phase (not the setup function - before even that runs) - a process called "[crt0](http://en.wikipedia.org/wiki/Crt0)" for "C RunTime phase 0". This copies all the values for the pre-set (static and global) variables from flash into RAM, and that is the only time these initialization values are ever assigned.
> 
> In effect the "=0" of your static declaration is not performed there, but right at the start of the program before even setup() runs.

Good description, but comparing static to global , even as in functionality only, is IMHO iffy.  
The main purpose of "static" variable is - it gets allocated and ASSIGNED once.  
And it has nothing to do with Arduino IDE Setup function, as you pointed out.  
And its accessibility / scope is same as any other variable - has nothing to do with "static" either.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 13, 2015, 3:02am UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631/12 "2015-09-13T03:02:39Z")

</div>

> firashelou:  
> i don't really get it,  
> i know from other languages that static or the world specific for that language means this variable can be used inside one function only, global means any functions can use it
> 
> i made a little search i found on a forum that they say a static variable means it can be used in 1 function only the one declared in, but can not be used in another unless it's not static
> 
> about your example i don't get it ! why is it .cpp and not .ino ?  
> and why would i have multiple files ?! you mean multiple tabs ?

and why would i have multiple files ?! **you mean multiple tabs**?

Unfortunately yet another Arduino.inc coined term.

"Tab" is common name for GUI control, file contains your code.

Using multiple files is beneficial in organizing your code into logical, easier to manage units - for example one file blinks LED and another writes to LCD , yet another keeps your robot moving etc.

You can share files between applications - "code once , reuse many times " as another somewhat forgotten way to code software. Or you can call it "cut and paste" if you prefer.

---

<div class="post-metadata">

**Author:** ![RayLivingston](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@RayLivingston](https://forum.arduino.cc/u/RayLivingston)\
**Post date:** [September 13, 2015, 3:04am UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631/13 "2015-09-13T03:04:12Z")

</div>

> Vaclav:  
> And its accessibility / scope is same as any other variable - has nothing to do with "static" either.

No, that is not right. It absolutely DOES limit scope to the current "block". A static variable placed into a .c or .cpp file outside any function or block will be visible ONLY within that one file, and not to any other files linked into the build. In c/c++, using static forces BOTH persistence, AND limited scope.

Regards,  
Ray L.

---

<div class="post-metadata">

**Author:** ![nickgammon](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nickgammon](https://forum.arduino.cc/u/nickgammon)\
**Post date:** [September 13, 2015, 4:18am UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631/14 "2015-09-13T04:18:16Z")

</div>

> Vaclav:  
> and why would i have multiple files ?! **you mean multiple tabs**?
> 
> Unfortunately yet another Arduino.inc coined term.
> 
> "Tab" is common name for GUI control, file contains your code.

Unfortunately, "tab" is a word with multiple meanings. For example you have a Tab key on your keyboard. You can "pay your tab" at the bar.

In the IDE if you make multiple tabs they become multiple files in the sketch directory.

See [https://en.wiktionary.org/wiki/tab](https://en.wiktionary.org/wiki/tab)

---

<div class="post-metadata">

**Author:** ![pYro\_65](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/pyro_65/32/214665_2.png) [@pYro\_65](https://forum.arduino.cc/u/pYro_65)\
**Post date:** [September 13, 2015, 4:33am UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631/15 "2015-09-13T04:33:40Z")

</div>

> majenko:  
> In effect the "=0" of your static declaration is not performed there, but right at the start of the program before even setup() runs.

Bang! Relying on this statement will hurt one day.

What you are seeing is the result of link time optimizations. You should only expect the value to be set when the declaration is first encountered in normal code flow.

```arduino
int func(){
  Serial.print("Or is this?");
  return 1;
}

void setup() {
  Serial.begin(9600);
  Serial.println("Is this printed first?");
}

void loop() {
  static int i = func();
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 13, 2015, 3:41pm UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631/16 "2015-09-13T15:41:12Z")

</div>

> RayLivingston:  
> No, that is not right. It absolutely DOES limit scope to the current "block". A static variable placed into a .c or .cpp file outside any function or block will be visible ONLY within that one file, and not to any other files linked into the build. In c/c++, using static forces BOTH persistence, AND limited scope.
> 
> Regards,  
> Ray L.

Ray, you sure like to split hair - "the scope of the static (variable) IS SAME as any other variable" .  
Or - the scope of static ( variable) follows SAME rules as any other variable.  
No need to muddy the waters adding blocks, files , functions etc.  
But , whatever floats your boat.

---

<div class="post-metadata">

**Author:** ![RayLivingston](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@RayLivingston](https://forum.arduino.cc/u/RayLivingston)\
**Post date:** [September 13, 2015, 4:43pm UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631/17 "2015-09-13T16:43:58Z")

</div>

> Vaclav:  
> Ray, you sure like to split hair - "the scope of the static (variable) IS SAME as any other variable" .  
> Or - the scope of static ( variable) follows SAME rules as any other variable.  
> No need to muddy the waters adding blocks, files , functions etc.  
> But , whatever floats your boat.

It's not splitting hairs. You clearly don't understand the full effect of static. It absolutely DOES change the scope in some cases. An "int" and a "static int" at file scope do NOT have the same scope.

Regards,  
Ray L.

---

<div class="post-metadata">

**Author:** ![firashelou](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@firashelou](https://forum.arduino.cc/u/firashelou)\
**Post date:** [September 13, 2015, 9:30pm UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631/18 "2015-09-13T21:30:03Z")

</div>

> [@](#):
>
> Unfortunately, "tab" is a word with multiple meanings. For example you have a Tab key on your keyboard. You can "pay your tab" at the bar.
> 
> In the IDE if you make multiple tabs they become multiple files in the sketch directory.
> 
> See [https://en.wiktionary.org/wiki/tab](https://en.wiktionary.org/wiki/tab)

ok so what the guys mean is this right ? multiple files in one folder ?  
and how linked the files ? is it by just typing this at the start : #file ?

---

<div class="post-metadata">

**Author:** ![firashelou](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@firashelou](https://forum.arduino.cc/u/firashelou)\
**Post date:** [September 13, 2015, 9:31pm UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631/19 "2015-09-13T21:31:26Z")

</div>

**ok guys, i really got more confused now !**  
**i need the know the difference in a simple way and not using very technical high class words 😕**

---

<div class="post-metadata">

**Author:** ![guix](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/guix/32/215371_2.png) [@guix](https://forum.arduino.cc/u/guix)\
**Post date:** [September 13, 2015, 10:39pm UTC](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631/20 "2015-09-13T22:39:17Z")

</div>

Maybe look at some tutorials, for example: [The Static Keyword in C++ - Cprogramming.com](http://www.cprogramming.com/tutorial/statickeyword.html)

[Next page](https://forum.arduino.cc/t/difference-between-static-int-and-int/166631.md?page=2)
