simple question about variable creation and initialization in loop()

I am very much an amateur with all things programming related so I am curious about the normal use of the static keyword when declaring a variable. I have slightly more experience programming with avrstudio in C whereby in my main function I will first declare my variables and then use a 'while' loop to cycle through my code that actually has action items. I notice that the equivalent process in arduino is to declare my variables all as static to avoid having the loop function create and initialize them on each iteration through my loop. Is this pretty typical to declare all variables in loop as static if you don't want to reinitialize them every time? For example it seems that:

void main()
{
int i = 0;
int j = 10;

while(1)
{
i++;
j--;
}
}

is not equivalent to:

void loop()
{
int i = 0;
int j = 10;

i++;
j--;

}

but is equivalent to:

void loop()
{
static int i = 0;
static int j = 10;

i++;
j--;
}

Unless you have an explicit need to declare local variables and make them static, it's more usual in Processing to declare them globally at the start of the code before setup().

Is this pretty typical to declare all variables in loop as static if you don't want to reinitialize them every time?

Either declare them as static or declare them globally.

Your examples are functionally correct.

You might be interested in this thread:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1283329855

Turns out, void loop() is actually inside of an infinite for-loop.

Thanks for the replies. I guess I have always tried to limit the use of global variables only because I had read somewhere that it is poor form to declare lots of global variables. Of course that was programming a simple application in C# so perhaps that doesn't apply here.

void main()
    {
    int i = 0;
    int j = 10;

    while(1)
        {
        i++;
        j--;
        }
    }

is not equivalent to:

void loop()
    {
    int i = 0;
    int j = 10;

    i++;
    j--;
    }

because in the first example the variables never go "out of scope". To write the equivalent main() you would write:

void main()
    {
    while(1)
        {
        int i = 0;
        int j = 10;

        i++;
        j--;
        }
    }

In this code:

while(1)
  {
  i++;
  j--;
  }

It doesn't really matter where the variables are declared. The loop will never exit.

In fact the compiler is smart enough to realize that code doesn't do anything except loop, and optimizes away everything except the infinite loop:

Example code:

void setup()
{
  int i = 0;
  int j = 10;

  while(1)
  {
    i++;
    j--;
  }
}

void loop () { }

Generates for "setup" this single instruction:

void setup()
  a6:	ff cf       	rjmp	.-2      	; 0xa6 <setup>

000000a8 <loop>:
    j--;
  }
}