This code shows, in the serial monitor, just "ab" I expected "abcdef"
Anyone know why?
I start the serial monitor as soon as I can after compiles, but is that small lapse the problem?
It is never, ever correct to call exit() within an Arduino sketch. Your sketch does not run as a process within an operating system so it makes no sense to exit the process; you can't return control to the bootlloader.
If you want your sketch to simply stop, call an infinite loop; it will remain in the loop until the Arduino is reset. Alternatively, you could put the Arduino to sleep - although the extra effort is pointless unless you are trying to save power or something. If you only want the sketch to do something once then follow UKHelliBob's suggestion and put the code to do it in setup().
It is never, ever correct to call exit() within an Arduino sketch. Your sketch does not run as a process within an operating system so it makes no sense to exit the process; you can't return control to the bootlloader.
What does happen to control when there is a call to exit()?
johnkauffman: <posts code that calls exit()>
PeterH: "Don't ever use exit(), the correct way to halt is to call an infinite loop"
econjack: "What does exit() do anyway?"
pYro_65, WizenedEE: "calls an infinite loop"
johnkauffman: <posts code that calls exit()>
PeterH: "Don't ever use exit(), the correct way to halt is to call an infinite loop"
econjack: "What does exit() do anyway?"
pYro_65, WizenedEE: "calls an infinite loop"
Comedy gold fellas.
Well they are different in that calling a infinite loop method still allows interrupts to continue thus updating millis() counter, any active PWM outputs continue refreshing, any active servo outputs continue refreshing, and any incoming serial data will still be received and stored into a ring buffer. The second method callin exit() will stop all those processes because it disables all interrupts.
When you enter an infinite loop, the behaviour is obvious and predictable.
When you call exit(), the conventional semantics of exit make no sense in this context and the actual behaviour is not at all obvious, and not defined anywhere except in the source code of the AVR library. I stand by my comment that there is no situation where it is appropriate to call exit() in an Arduino sketch. The concept of exiting simply doesn't translate to an Arduino environment; there is nothing to exit to.
exit() on a PC means "everything in this program stops"
exit() on the arduino means "everything in the program stops"
I fail to see any confusion, except when people assume that things are synchronous when they are not (and the same bug in the OP could have appeared on a PC if it relied on another thread doing stuff)
WizenedEE:
With your version, the code posted in the OP would work.
Yes, you are correct interrupts are disabled, I assume the OP got the general gist of my comment though.
The concept of exiting simply doesn't translate to an Arduino environment; there is nothing to exit to.
Not going to name names, but I use an operating system that occasionally likes to exit out to nowhere by showing a lovely blue screen when it can't cope any more.
pYro_65:
Not going to name names, but I use an operating system that occasionally likes to exit out to nowhere by showing a lovely blue screen when it can't cope any more.
Many here have the same experience, but I wouldn't call that blue screen 'lovely'.