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().
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.