exit() seems premature

I don't want my code to run forever, just once. So I am ending the main loop().

void setup(){Serial.begin(9600);}
void loop(){
Serial.println("abcdef");
exit(0);
Serial.println("zyxwvuts");
}

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?

Serial.print doesn't actually print, it just puts the text in a buffer and arranges for it to be printed later.

Call Serial.flush() before exit().

If you want your code to run once put it in the setup() function.

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()?

econjack:
What does happen to control when there is a call to exit()?

Nothing useful.

while(true);

Nothing useful.

I was just curious, as it does seem to end the loop.

To the same end, I once saw this infinite loop in code:

#define ever ;;

// some code...

for(ever) {
   // more code...
}

Lots of people out there with way too much time on their hands...

As already stated if you want something only performed once just place it in the setup function and leave the loop() function empty.

void setup()
  {
    Serial.begin(9600);
    Serial.println("abcdef");
    Serial.println("zyxwvuts");
  }
void loop() {}

Thanks for answers. I appreciate both the code and the theory behind it.

pYro_65:

econjack:
What does happen to control when there is a call to exit()?

Nothing useful.

while(true);

it's actually

while(1) cli();

The actual source is

_U(exit):
	cli
	XJMP	_U(_exit)

With your version, the code posted in the OP would work.

I wholeheartedly endorse this thread.

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.

crimony:
I wholeheartedly endorse this thread.

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.

Lefty

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