Did I find a bug? Huh?

byte variable1;
byte variable2;
byte variable3;
byte variable4;

void setup() {

  Serial.begin(115200);
}

void loop() {


  byte variablesOccupied;

  if (variable1 > 0) {
    variablesOccupied++;
  }
  if (variable2 > 0) {
    variablesOccupied++;
  }
  if (variable3 > 0) {
    variablesOccupied++;
  }
  if (variable4 > 0) {
    variablesOccupied++;
  }

  variable1 = 0;
  variable2 = 0;
  variable3 = 25;
  variable4 = 0;


  Serial.println(variablesOccupied);
  delay(100);
}

In this case, I should get an output of "1" because only variable3 is occupied (>0).
But I am getting
241
241
233
233
233
233
233
233

Where does that come from?
But if I place
variable1=0;
variable2=0;
variable3=25;
variable4=0;
at the beginning of the code like this:

byte variable1;
byte variable2;
byte variable3;
byte variable4;

void setup() {

  Serial.begin(115200);
}

void loop() {

  variable1 = 0;
  variable2 = 0;
  variable3 = 25;
  variable4 = 0;

  byte variablesOccupied;

  if (variable1 > 0) {
    variablesOccupied++;
  }
  if (variable2 > 0) {
    variablesOccupied++;
  }
  if (variable3 > 0) {
    variablesOccupied++;
  }
  if (variable4 > 0) {
    variablesOccupied++;
  }




  Serial.println(variablesOccupied);
  delay(100);
}

Then I get:

1
1
1
1
1
1
1
As expected.
Why is there random data in"variablesOccupied"?
After one code loop, the "variablesOccupied" function should get correct information from the global variables (variable1, variable2, variable3, and variable4) even though "byte variablesOccupied" is declared locally. isn't a local variable reset to '0' every loop?

Can somebody please explain.
Thanks

In this case, I should get an output of "1" because only variable3 is occupied

Each time through loop() variablesOccupied is declared and because it is a local variable there is no guarantee what value it will be set to. Either declare it as static or initialise it to a value of zero

And if you have the compiler warning level set to 'All' you would have seen;

warning: 'variablesOccupied' may be used uninitialized in this function [-Wmaybe-uninitialized]

Warning you of the mistake you might be about to make.

Did I find a bug? Huh?

It is not impossible but it is so unlikely that it is not worth asking that question. :slight_smile:

...R

Nope, pilot error.

Thanks!

https://www.arduino.cc/en/Reference/VariableDeclaration

A variable is a way of naming and storing a value for later use by the program, such as data from a sensor or an intermediate value used in a calculation.

Declaring Variables
Before they are used, all variables have to be declared. Declaring a variable means defining its type, and optionally, setting an initial value (initializing the variable). Variables do not have to be initialized
(assigned a value) when they are declared, but it is often useful.

int inputVariable1;
int inputVariable2 = 0; // both are correct

Is this true only for global variables?

Global and static variables can be initialised.

Your variablesOccupied could be simply qualified “static”

So

  1. global variables do not have to be "static" as they are not subject to random change from other parts of code / memory.
  2. Local must be assigned a value or be "static" otherwise run the risk of it being assigned a random value.

Correct?

Rule #1 of Newbies and compilers - You will NOT find a compiler bug. Your code is flawed.

Regards,
Ray L.

Globals can be global and static, meaning their scope is limited to that compilation unit.
Local variables don’t have to be initialised when they are defined, but it often helps if they are.

I have a pet hate of functions with ten lines of local variable definitions, and ten more lines of initialisation.

It’s best to initialize all of your variables.

If you do not initialize global variables they are set too zero for you.

Local variables have junk in them at initialization, you should keep this in mind and may want to initialize them to a value.

Static variables retain their value when their function exits.

You don't have to initialize stack variables. You also don't have to release the handbrake before backing the car out of the driveway.

larryd:
It’s best to initialize all of your variables.

If you do not initialize global variables they are set too zero for you.

Local variables have junk in them at initialization, you should keep this in mind and may want to initialize them to a value.

Static variables retain their value when their function exits.

Useful answer. Well explained. Thanks!

PaulMurrayCbr:
You don’t have to initialize stack variables. You also don’t have to release the handbrake before backing the car out of the driveway.

You don’t have to release the handbrake before backing the car out of the driveway. But you will run into issues.
My question was if Declared Global vars are automatically initialized to / assigned a value of 0. vs. locally declared variable may have some garble until initialized.

@larryd explained it well

Any C/C++ reference will cover this, prominently.

aarg:
Any C/C++ reference will cover this, prominently.

I looked it up. Interesting.
Now I understand

for (int i=0; i <= 255; i++) {
  }

VS

for (int i; i <= 255; i++) {
  }

larryd:
It’s best to initialize all of your variables.

Indeed so.

If your cunning wheeze program depends on a variable initialising with a specific value, make sure you do initiase it to that value.