Timeline for Variable declaration inside main loop
Current License: CC BY-SA 4.0
18 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jun 30, 2019 at 22:28 | comment | added | marcelm |
@Sören "But in a C-program a variable declared in main() keeps its value." - A variable in main() keeps it value until execution leaves main(). Just as for Arduino's loop(), and indeed any other function. (One difference of course is, that once you leave main(), your program exits and it doesn't matter what happens to the variables.)
|
|
| Jun 30, 2019 at 18:43 | history | became hot network question | |||
| Jun 30, 2019 at 18:28 | answer | added | Jeff Wahaus | timeline score: 2 | |
| Jun 30, 2019 at 12:42 | comment | added | Michel Keijzers | @EdgarBonet Thanks for that code. | |
| Jun 30, 2019 at 12:41 | comment | added | Edgar Bonet |
@MichelKeijzers: Here is main().
|
|
| Jun 30, 2019 at 12:38 | comment | added | Michel Keijzers | (btw if an answer is helpful, upvote it (by clicking the 'up' arrow), if an answer (or the best answer is given that solves your problem), accept the answer. | |
| Jun 30, 2019 at 12:36 | comment | added | Michel Keijzers | Yes you are right (if I follow your sentences correctly). I'm not fully sure, but I guess the main() which is mandatory for c is created by the IDE and invisible for the user, it initializes something, than calls setup (once), than maybe some other checks are done, than a `while (true) { loop(); } is called, | |
| Jun 30, 2019 at 11:00 | comment | added | Sören | Just to get this straight: With the arduino the main loop() just is a regular C-function with all the implications regarding variable scope. But in a C-program a variable declared in main() keeps its value. So if I used plain C for my program instead of the Arduino framework the counter variable would work without being declared static. Of course then the if-statements would be located in a while(1) loop. | |
| Jun 30, 2019 at 10:35 | answer | added | Duncan C | timeline score: 4 | |
| Jun 30, 2019 at 10:35 | review | Close votes | |||
| Jun 30, 2019 at 17:55 | |||||
| Jun 30, 2019 at 10:35 | comment | added | Sören | Thank you very much for the explanation and possible solutions. | |
| Jun 30, 2019 at 10:29 | history | edited | Michel Keijzers |
edited tags
|
|
| Jun 30, 2019 at 10:29 | comment | added | Michel Keijzers | No they are not, afaik the loop() is just a function that has a while(true) or while(1) inside. | |
| Jun 30, 2019 at 10:27 | comment | added | Sören | So the main loop in the Arduino just behaves as any regular function that is called? For some reason I thought that variables declared in main() are automatically static and keep their value. | |
| Jun 30, 2019 at 10:20 | comment | added | Michel Keijzers | This question fits better in stack overflow, since it is not related to Arduino. | |
| Jun 30, 2019 at 10:20 | review | First posts | |||
| Jun 30, 2019 at 12:19 | |||||
| Jun 30, 2019 at 10:19 | answer | added | Michel Keijzers | timeline score: 0 | |
| Jun 30, 2019 at 10:17 | history | asked | Sören | CC BY-SA 4.0 |