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?
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.