Double print on upload

This seems like the right topic so here goes.

The below simple sketch:

// example/tutorial - https://www.georgevreilly.com/blog/2016/01/05/DecrementingLoops.html

uint32_t bigOne = 0x12345678;
byte pieces[4];

void setup() {
  Serial.begin(115200);
  
  memcpy(&pieces, &bigOne, 4); // 32-bit var moved to four consecutive byte array locations
 
  for (int8_t i = 4; --i >= 0;){ // decrementing loop
    Serial.print(pieces[i], 16);
    Serial.print(" ");
  }
Serial.println();
}

void loop() {

}

Produces this output upon upload (serial monitor already open):

If I clear the output window and press the reset on the controller board it prints a single line, as expected.

Why?

IDE v 1.8.13

double print.png

Probably the Uno gets reset once immediately after the upload, and then again when the Serial monitor is opened. The host computer buffers some of the output from in between the two resets. Since the auto-reset feature was designed to operate off of side-effects of opening and closing the serial port, this is difficult to avoid.

westfw: Since the auto-reset feature was designed to operate off of side-effects of opening and closing the serial port, this is difficult to avoid.

Thanks @westfw!