Usage of \b and \r in C

C

C Problem Overview


\b and \r are rarely used in practice. I just found out that I misunderstood these two escape sequences. A simple test:

printf("foo\bbar\n");

I expected it to output fobar, because \b will backspace the cursor, and b will overwrite the second o, but instead it outputs: foobar

The same is with \r:

printf("foo\rbar\n");

I thought \r will move the cursor to the beginning of the current line, so bar will replace foo, so the final output should be bar. However, it actually outputs:

foo
bar

C Solutions


Solution 1 - C

The characters will get send just like that to the underlying output device (in your case probably a terminal emulator).

It is up to the terminal's implementation then how those characters get actually displayed. For example, a bell (\a) could trigger a beep sound on some terminals, a flash of the screen on others, or it will be completely ignored. It all depends on how the terminal is configured.

Solution 2 - C

The characters are exactly as documented - \b equates to a character code of 0x08 and \r equates to 0x0d. The thing that varies is how your OS reacts to those characters. Back when displays were trying to emulate an old teletype those actions were standardized, but they are less useful in modern environments and compatibility is not guaranteed.

Solution 3 - C

The interpretation of the backspace and carriage return characters is left to the software you use for display. A terminal emulator, when displaying \b would move the cursor one step back, and when displaying \r to the beginning of the line. If you print these characters somewhere else, like a text file, the software may choose. to do something else.

Solution 4 - C

I have experimented many of the backslash escape characters. \n which is a new line feed can be put anywhere to bring the effect. One important thing to remember while using this character is that the operating system of the machine we are using might affect the output. As an example, I have printed a bunch of escape character and displayed the result as follow to proof that the OS will affect the output.

Code:

#include <stdio.h>
int main(void){
    printf("Hello World!");
    printf("Goodbye \a");
    printf("Hi \b");
    printf("Yo\f");
    printf("What? \t");
    printf("pewpew");
    return 0;
}

Solution 5 - C

As for the meaning of each character described in C Primer Plus, what you expected is an 'correct' answer. It should be true for some computer architectures and compilers, but unfortunately not yours.

I wrote a simple c program to repeat your test, and got that 'correct' answer. I was using Mac OS and gcc. enter image description here

Also, I am very curious what is the compiler that you were using. :)

Solution 6 - C

In different compilers, backslash escape characters behave differently.

In MAC OS with clang \b takes off the last char from the console as you expected.

#include <iostream>
int main ()
{
  std::cout << "Test";
  std::cout << "\b";
  std::cout << "\n";
  return 0;
}

output:

 Tes

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYu HaoView Question on Stackoverflow
Solution 1 - CComicSansMSView Answer on Stackoverflow
Solution 2 - CMark RansomView Answer on Stackoverflow
Solution 3 - CJoniView Answer on Stackoverflow
Solution 4 - CMoh KView Answer on Stackoverflow
Solution 5 - CJacky WangView Answer on Stackoverflow
Solution 6 - CsepidehaView Answer on Stackoverflow