difference between static int and int

arslankhan:
every time when myfunction() runs it comiles static int my_static=0; then how my_static can be increased?

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

my_static is increasing, so what is the problem?

Do you mean why is it increasing? Because a static variable inside a function is only initialized once, and after that it retains its value. The non-static variable is initialized every time you call the function.

firashelou:
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 :confused:

I just realized this thread has been hijacked from "static" to "tabs".
I hope I do not get flamed for leaving this post intact, as is.
But I'll not continue discussing "tab" here anymore.
Fair?

I do not believe anybody is trying to impress you by using the right terminology in description.
Lowering the description to lower class words does not help you if you are in for a long haul.

So - again "Tab" in computer usage is a name of GUI ( graphical user interface ) of a single functionality - it is named "control". Your have simplified / modified "pull down" control (upper right corner of IDE") that activates yet another control "menu".

When you open IDE it will start with default x.ino file and by using the "pull down " control to activate the "menu" and than selecting menu item "New Tab" you add another file to your application.
The new file is now accessible ( for editing) by using the "tab" on "status bar" or by using the "pull down " control. But it is a file , not a tab.

To be able to compile both x.ino and the new file you need to tell the x.ino to add the new file to it - hence you will use "preproccessor directive " #include" to accomplish that.

Hopefully now you can see how important using the right terminology is - you do not "link" the new file to the x.ino - you are "including" the new file (text) with x,ino file (text). .

Still confused?

Post another question, there are many capable people here to help you, and with better command of English than I have.

That's only partly correct. I describe the process here (see "How the IDE organizes things").

If you make a .cpp file as a separate tab in the IDE, it is compiled as a separate file, and that is where static is relevant.

However I won't go on about it here, because this is not what the question was about.

thanks guys,
so static will let me use the variable only in the file it is declared in ?
global will let me use this variable in another "included" file ?

firashelou:
global will let me use this variable in another "included" file ?

Another linked file.

I don't know what's so puzzling about static or not.

For a function

void foo() {
  int ival=0;
  ival++;
  Serial.print("foo ival="); Serial.println(ival);
}

the variable ival is reset to 0 at each call, and then incremented.
Instead, by

void bas() {
  static int ival=0;
  ival++;
  Serial.print("bas ival="); Serial.println(ival);
}

the local variable ival keeps it's old value when bas() is finished and when bas() is called again it will start incrementing by it's last one.

the Static Keyword You Don’t Fully Understand @ Hackaday

septillion:
the Static Keyword You Don’t Fully Understand @ Hackaday

thanks a lot !! this article is damn amazing ! now i got it :slight_smile:

one thing :
so i can name a static variable at the top level of the file and then in the same file inside a function i can name the same name of variable by declaring it static ?

Declaring a static at global level is not needed, they already live the whole program long.

And if you name a variable inside a function the same as a global you just access the local variable inside the function (and are unable to just call the global). Static or not.

septillion:
Declaring a static at global level is not needed, they already live the whole program long.

And if you name a variable inside a function the same as a global you just access the local variable inside the function (and are unable to just call the global). Static or not.

yes in a function yes just the function has access but i was talking just about the name

septillion:
Declaring a static at global level is not needed, they already live the whole program long.

And if you name a variable inside a function the same as a global you just access the local variable inside the function (and are unable to just call the global). Static or not.

You can access the global version by giving a fully qualified name (specify global scope).

int foo;

void func(){
  int foo;

  Serial.print( foo );   //Local foo

  Serial.print( ::foo ); //Global foo
}

septillion:
Declaring a static at global level is not needed, they already live the whole program long.

Declaring a global variable "static" is not needed, but it's useful to restrict the variable to file scope so as not to pollute the global variable namespace.

septillion:
Declaring a static at global level is not needed, they already live the whole program long.

However doing so hides it from the linker so that it cannot be linked into a different compilation unit.

Let me show you.

Make a new sketch:

int foo;

void setup () { }
void loop () { }

That compiles OK.

Now add a new tab to it, call it bar.cpp. Put in bar.cpp:

int foo;

Now compile. You get a linker error:

bar.cpp.o:(.bss.foo+0x0): multiple definition of `foo'
sketch_sep28a.cpp.o:/home/nick/Development/arduino-1.0.6/sketch_sep28a.ino:4: first defined here

Now if you make either of those "int foo" to be:

static int foo;

Then it compiles OK again. This is because they are now local to the compilation unit.


You might say, "so what?", but if multiple files have, say:

int count;

Then you could get a linker error because they all try to use the same "count" variable. So things like libraries should declare "local" (to the library) global variables as static, to avoid this.

great explanation to be added thank you :slight_smile:

@pYro_65, I know, that's why I say, "just call". You have to call it explicit as a global.

@Nick, didn't knew that! So the static one becomes the local of the file and the non static becomes the global? Nice to know :slight_smile:

Al tough I wouldn't make a include with a variable name that generic in global scope...

septillion:
@pYro_65, I know, that's why I say, "just call". You have to call it explicit as a global.

Well, Why didn't you mention this if you knew... Your use of the word 'unable' shows otherwise.
And I'm sure other people may find it useful to know that it is possible, no need to withhold information.

If I have to explain every question here into the every little detail it would be a complete day job. :wink: My statement was to explain the normal way of calling to the variable does not work because the local variable is checked first (static or not). I thought I nuanced it enough by stating "just call". I did not explain it any further because I think it makes bad code to have a local and global var with the same name. So because you can does not mean you should...

septillion:
I think it makes bad code to have a local and global var with the same name. So because you can does not mean you should...

And when you use multiple namespaces using common identifiers, do you simply give up because you don't want to qualify them...

There are times when you have no control over the identifiers used. Your argument is a little short sighted.

No, of course not! I just simplified the answer to what I thought was relevant + hints there is more. But if the OP want to know more he has to ask or just Google it.

Now stop being an ass. I'm not doubting you or something. We're all here just to help. And we all make decisions on what's relevant info. If you think you need to add info, fine! No problem. But don't be a dick to others. :wink:

septillion:
No, of course not! I just simplified the answer to what I thought was relevant + hints there is more. But if the OP want to know more he has to ask or just Google it.

Now stop being an ass. I'm not doubting you or something. We're all here just to help. And we all make decisions on what's relevant info. If you think you need to add info, fine! No problem. But don't be a dick to others. :wink:

I did add info, and you took it upon yourself to assume it was me questioning you, simply because I quoted you and provided additional information, so I just fulfilled that. You said it was bad practice and I pointed out a situation where it isn't :slight_smile:

But seriously, "just call" does no justice for newbies, they aren't mind readers, and probably aren't cryptic crossword enthusiasts... hence why my post was relevant!