Pages: [1]   Go Down
Author Topic: Initiallizing local variables  (Read 460 times)
0 Members and 1 Guest are viewing this topic.
Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25773
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

TIP: Google about the scope rules of variables in C.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

No you don't. There are lots of different static so keep an open mind for next time it may mean something else.
If you want a variable only that function needs to access, you can use static. Non static local variables are released after function call so memory is free for other things. Static variables are kept so don't define too many of them.
Logged


Pages: [1]   Go Up
Jump to: