10 years, 4 months ago.

printf staggering results on new lines

Greetings,

I have a piece of code:

  1. include "mbed.h"

DigitalOut led_red(LED_RED); DigitalOut led_green(LED_GREEN); Serial pc(USBTX, USBRX);

int main() { while (true) { pc.printf("The color is green.\n"); led_red = 0; led_green = 1; wait (1); pc.printf("The color is red.\n"); led_green = 0; led_red = 1; wait (1); } }

When I run this, I get the LEDs blinking and I get a result in putty, my term emulator. However, in putty, I see the lines staggering instead of all lining up on the left. I would expect to see them one below the other. However, I am seeing them one below the other offset by the amount of characters of the preceding line.

Why would that be? Isn't the \n supposed to be a carriage return and linefeed? Shouldn't I now be starting at the first position on each line?

Thanks!

1 Answer

10 years, 4 months ago.

That depends on the settings of your terminal. Generally they can be set so \n is both carriage return and linefeed, but most by default only use it as linefeed. Either you can change the settings (dunno where in putty that would be), or you can explicitly use \r\n (what I generally do).

Accepted Answer