String output using "\r" and "\n"

I’ve always used “\r” as line break on mac but lately (big sur, m1) I’m having some weird issues when printing strings using “\r”
like

        string m = "";
        m += "testing string new line\r";
        m += "and another line\r";
        cout << m << endl;

works OK in XCode output, but not in Bash or Zsh
the output is just one line with some characters of the previous lines if they are larger, like

and another lineew line

using “\n” instead solves but I’m intrigued about that. anybody else noticed this change?
are terminals now treating “\r” as carriage return, literally like a typewriter?

Yes the terminals you use for bash/zsh are literally based off physical terminals. AFAIK they have always treated \r as a carriage return although you can configure behaviour with stty and the icrnl option.

on windows newlines are specified as ‘\r\n’ on unix-likes (incl osx) ‘\n’ is fine. that is why c++ has std::endl so it is a platform independent way of making a new line.

1 Like