main() != loop()

Well, I understand that the subject is not a revolutionary discovery. We know about setup() and so on. But I used to believe these are similar constructions:

setup() {
  setupcode;
}
loop() {
  loopcode;
}

and

main() {
  setupcode;
  while( 1) {
    loopcode;
  }
}

But now I’ve got some doubts. It’s about the scope of variables. Here is a very simple sketch:

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

byte a = 0;

void loop() {
  byte b = 0;
  a ^= 1;
  b ^= 1;
  Serial.print( a, DEC);
  Serial.print( " ");
  Serial.println( b, DEC);
  delay( 1000);
}

I expected to see
1 1
0 0
1 1
0 0

and so on. Instead, I obtained:

1 1
0 1
1 1
0 1

The local variable “b” is initialized in every cycle! And, as far as I know, the local var in main() can be initialized once (if it is outside loop):

char a = 0;
int main()
{
  char b = 0;
  while(1) {
    a ^= 1;
    b ^= 1;
  }
}

Now my question to educated fellows is:

NO local variables in loop() can be initiated only once - am I right?

Thanks!

This is how the generated code looks like:

include <WProgram.h>

void setup()
{
}

void loop()
{
}

int main(void)
{
  init();
  setup();
  
  for (;;)
    loop();
  
  return 0;
}

As you see from above, neither loop nor setup is in the scope of main.

NO local variables in loop() can be initiated only once - am I right?

Well...

void loop() {
  static int initOnce = 5;

  Serial.println(initOnce);
  initOnce = 4;

}

Thank you for answers!

So, if I add some declarations to the BenF’s code, we’ll have:

void setup()
{
}

char _global;

void loop()
{
  char _local;
  static char _static;
}

it will become

char _global;

int main(void)
{
  init();
  setup();
  
  /*static*/ char _static;
  for (;;)
    char _local;
    loop();
  
  return 0;
}

It’s very good to understand! Can you tell me, where are the rules of preprocessing Arduino (Wire) code are formally described?

And where can I find a list of compilation (linking, loading) errors?

One more question. What are the instruments of debugging Arduino sketches? Anything similar to AVR Studio? Or only with real devices?

Thanks!

… not quite.

This is how it will look like:

include <WProgram.h>

void setup()
{
}

char _global;

void loop()
{
  char _local;
  static char _static;
}

int main(void)
{
  init();
  setup();
  
  for (;;)
    loop();
  
  return 0;
}

Thank you! Please tell me, how can I look preprocessed program, that is passed to GCC? This example you've showed - is it just the power of your imagination or some instruments are available?

I think you can simply look in the in TEMP dir, under windows it is called build768686868xxxx or something. It will contain the post processed CPP file for the sketch.

You’ll find “main.cpp” in hardware/arduino/cores/arduino

I think you can simply look in the in TEMP dir,

Please, where is such a directory?

Somewhere in a random-folder. After Compiling, the ArduinoIDE tells you the path, written in red.

Somewhere in a random-folder. After Compiling, the ArduinoIDE tells you the path, written in red.

Only shows location if you press the shift key when you verify or upload the sketch.

Only shows location if you press the shift key when you verify or upload the sketch.

Cool, didn't know that (+1) :D