Float print oddity

Working on another little project I found this incorrect output:

//
void setup() {
  Serial.begin(115200);
  float floatVar = 30.141;
  float newVar = 4.275;
  //    Serial.println(floatVar);
  Serial.println(newVar, 3);
  Serial.println(floatVar);
}

void loop() {

}

UNO R3, IDE 1.8.10

The sketch above will not print newVar. If I remove the comment from the first floatVar print it won't print the first floatVar but the two following prints are executed.

What is going on?

The sketch above works fine for me. Either way.

I've noticed in quite a few of my sketches where I have something printing in setup shortly after begin that I don't see anything from the first print. Even with quoted constant strings. The first one won't print but the second one and all subsequent ones will.

Sometimes a short delay after begin seems to change things and sometimes not. I've just gotten into the habit of printing some garbage right after I call begin.

the behavior seems new because some of these codes used to run and print everything and now they miss the first line. I thought it might be my computer.

Loss of initial serial output frequently happens with the 32u4 processors, which take some time to connect with the USB virtual serial port.

Fix it with

while(!Serial); //after the Serial.init(baud);

jremington:
Loss of initial serial output frequently happens with the 32u4 processors, which take some time to connect with the USB virtual serial port.

Fix it with

while(!Serial); //after the Serial.init(baud);

Mine is having problems on 1284P running MightyCore. It's on a Ubuntu 18.04 machine.

jremington:
Fix it with

while(!Serial); //after the Serial.init(baud);

That doesn't work for me. It's as if it's not even there.

Delta_G:
I've just gotten into the habit of printing some garbage right after I call begin.

This is the only thing that works on its own:

//
void setup() {
  Serial.begin(115200);
  float floatVar = 30.141;
  float newVar = 4.275;
//   while(!Serial);  // no effect
  Serial.println(""); // the cure
  Serial.println(floatVar);
  Serial.println(newVar, 3);
  Serial.println(floatVar);
}

void loop() {

}

I also discovered that if the first print after download doesn't work it will if the processor reset button is subsequently pressed.

Thanks for the replies and suggestions. I hadn't seen this before but now I know how to get around it.

Karma++.

Also, prints fine for me in NANO + IDE 1.8.12.
smp.png

smp.png

  Serial.println(""); // the cure

That's not a cure. That's a workaround.

And the empty string in there looks stupid.

PaulS:

  Serial.println(""); // the cure

That's not a cure. That's a workaround.

The pertinent part is 'works'.

PaulS:
And the empty string in there looks stupid.

Got anything constructive to add?