0

I have an embedded systems course in my university and i have a weird question that i don't know the answer to the question gives us the code (i may have a syntax error but the logic is correct)

void modify() {
    static volatile int counter = 0;
    printf(counter++);
}

int main() {
    modify();
    modify();
}

and the question asks "For the following code, True or False and justify: the program output will always be 0 1, assume the program is stored on the flash memory and the program is executed from the start every time it is run" when i tried running a similar code on arduino it resetted and started from zero but i have this weird question in the reference and i feel they are similar (i have attached the question) Another question from my reference "INTRODUCTION TO EMBEDDED SYSTEMS A CYBER-PHYSICAL SYSTEMS APPROACH"

11
  • As far as C goes (never mind which implementation or how stored) counter is initiliased once. So the output from successive calls is 0 1 2 3 . . . etc. Commented May 31 at 19:02
  • 1
    I reset the arduino using the button so it restarts and prints (0 1) then after resetting it again using red button it prints 0 1 again which i believe makes sense but according to my reference screenshot i think it shouldn't do that or maybe what i am trying isn't what the question is about? Commented May 31 at 19:06
  • 3
    printf(counter++); is not a valid call for the standard C printf function. Commented May 31 at 19:28
  • 1
    @zeyaddaowd, "my question is about this code" and invalid printf(counter++); implies you should post true code and true output. If the output is not as desired, post the desired output. "i think it shouldn't do that" does not say what you think it should be. Commented May 31 at 19:52
  • 1
    Assuming you meant printf( "%d\n", counter++); or similar, then the output would be 0 then 1, the likely result of returning from main would be to either restart or halt indefinitely, so you might see 0 then 1 repeatedly or just once. Arduino is a different matter, it's setup/loop framework is not compatible with the code you posted. The framework includes a main that executes setup/loop. You ought to post the exact code you are asking about, not something merely resembling it. Statically allocated data as in your question and link are initialised once and retain thier value across calls Commented Jun 1 at 8:05

1 Answer 1

1

Yes, you will always get 0 1. When you restart microcontroller the program begins from the beginning as on any machine after restarting it.

If you want to keep data between restarts, you should take a look at EEPROM.h library and its functions like EEPROM.write() and EEPROM.read() .

Each time you start your program you need to read last value from there. And save sometimes while running your code. But try not to stress EEPROM memory and make your writes infrequent as possible.

Sign up to request clarification or add additional context in comments.

1 Comment

Except it is very common to have a "minimal"/"fast" start-up code in the CRT, which skips static initialization entirely. That is non-conforming behavior to ISO C, but none the less often the case with microcontroller CRTs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.