Go Down

Topic: Initiallizing local variables (Read 572 times) previous topic - next topic

ash901226

guys i was wondering about local variable
my question is something like this
I always and i mean almost always initialize my variable to be a global variable, but i was wondering could i initiate it in someway so it to become a local variable
heres an example:
Code: [Select]

int Count=0;
void setup()
{ yada yada yada}
void loop()
{
did something heres that causes
  Count++;
}


but could somehow i want it to be like this

Code: [Select]

void loop()
{
  int Count=0;
somthing happen somwhere here
and then Count++;
}


i know that if i do it as the second way that is initializing the Count in the loop section will make the Count value reset to 0 every time it loop

AWOL

Resetting the value may or may not be what you want; it isn't clear to me from your post.
Does the "static" keyword answer any of your questions?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

ash901226

Sorry AWOL if i'm not clear with my question, I want to know if i could initialize Count variable as a local variable inside the void loop()?
i what the value to be the last value that it has b4 the loop function loop around, i dont want it to be reset back to 0.

wildbill

The static modifier does what you want:
Code: [Select]
void loop()
{
static int Count=0;
somthing happen somwhere here
and then Count++;
}

ash901226

now i know what static does, so this way i do not need to declare to may global variable now huh.

Go Up