[solved/understood] int main() versus setup()/loop() - Serial.begin/print bug?

Hi,
The following code only prints "st" on a Nano clone and nothing more.

int main(void) {
  Serial.begin(9600);
  Serial.print("start ...");
  Serial.println("init");
  while(1);
  return 0;
}

This code prints "start ...init"

void setup() {
  Serial.begin(9600);
  Serial.print("start ...");
  Serial.println("init");
}
void loop() {}

Why does the main-version not print the text correctly?

Tested on IDE 1.6.9 and 1.8.2.
Thanks & best

Why? I don't know, but here's a clue.
Try this:

int main(void) {
  init();
  Serial.begin(9600);
  Serial.print("start ...");
  Serial.println("init");
  while(1);
  return 0;
}

The answer probably lies in the source code of that function.

Because if you use main() things like interrupts are not setup() for you. If you use loop() and setup() init() and initVariant() is called before setup()

From main.cpp

int main(void)
{
 init();

 initVariant();

#if defined(USBCON)
 USBDevice.attach();
#endif
 
 setup();
    
 for (;;) {
 loop();
 if (serialEventRun) serialEventRun();
 }
        
 return 0;
}

Thanks for the answers.
I wasn't aware of relevant settings done in init(). Now I'm aware :slight_smile:

Best