Why my static qualifier does'nt work

The Arduino reference says:
"The static keyword is used to create variables that are visible to only one function. However unlike local variables that get created and destroyed every time a function is called, static variables persist beyond the function call, preserving their data between function calls.

Variables declared as static will only be created and initialized the first time a function is called."

I understand that a static variable is initialized only once, when the function is called for the first time, and that its data will be preserved between function calls. So, why this does'nt work??

void setup() {
Serial.begin(9600);
}

void loop() {
printOneTime();
}

void printOneTime() {
static int done;
if (done == 0) {
Serial.println("Printed one time");
done = 1;
}
}

What does it do that you think is wrong ?

I note that you did not initialise done with a value so that you don't actually know what its initial value is

You have created an uninitialized local variable. When you declare a global variable the variable is automatically set to a value of 0 if not given an initial value. Not so a static local variable. It will take on the value of whatever was in the memory location that gets assigned to the variable.

Read the forum guidelines to see how to properly post code and some information on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Yikes.
News to me!

How about that? Is it any better?

THanks. I re-started the Arduino IDE, and now it works.

I'm pretty sure static initialization occurs for static locals, just as it does for globals.

No, not really.

If "done" isn't initialised, shouldn't you at least get a warning "variable is used uninitialised in this function" ?

OK, I get it. I was assuming that an uninitialized static local variable and uninitialized local variable were the same as far as their initial value. I was wrong.

Here is a sample of what I found while looking for an answer:

It depends on the storage duration of the variable. A variable with static storage duration is always implicitly initialized with zero.

As for automatic (local) variables, an uninitialized variable has indeterminate value. Indeterminate value, among other things, mean that whatever "value" you might "see" in that variable is not only unpredictable, it is not even guaranteed to be stable. For example, in practice (i.e. ignoring the UB for a second) this code

I stand corrected and apologize for confusion.

Does this help:

char Incoming_value = 0;
                
void setup() 
{
    Serial.begin(9600);
    int16_t variable1 = 5000;
    int16_t variable2 = 10000;
    int16_t variable3 = 20000;
    Serial.println(variable1);
    Serial.println(variable2);
    Serial.println(variable3);
}

void loop()
{
    testFunction();
    while(1);
} 

void testFunction() {
    static int16_t variable1;
    static int16_t variable2;
    static int16_t variable3;
    Serial.println(variable1);
    Serial.println(variable2);
    Serial.println(variable3);
}
16:40:49.477 -> 5000
16:40:49.477 -> 10000
16:40:49.477 -> 20000
16:40:49.477 -> 0
16:40:49.477 -> 0
16:40:49.477 -> 0

The following sketch demonstrates that a static variable is initialized only once and peserves the value between function calls.

void setup()
{
  Serial.begin(9600);
  for (int i = 0; i < 3; i++)
  {
    printOneTime();
  }
}

void loop()
{

}

void printOneTime()
{
  static int done = 2;
  done++;
  Serial.println(done, DEC);
}

Output:

3
4
5

Re-run the sketch deleting the static qualifier. Then the following is the output which clearly establishes the role of the static keyword.

3
3
3

we still don't know what that means. What does not work?

In case there was any doubt, the code prints the message once. At least in my laboratory.

So @SMATHIEU13, what does it do when you run it, and what is it that you want it to do?

a7

I'm guessing the serial monitor is opened, code runs briefly, prints "Printed one time", gets reset, re-uploaded, and "Printed one time" again.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.