52

\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
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 7
    Where's the question? – Chris Jun 21 '13 at 13:19
  • what platform are you on? – suspectus Jun 21 '13 at 13:20
  • 1
    @Chris what does `\b` and `\r` do exactly? – Yu Hao Jun 21 '13 at 13:20
  • http://stackoverflow.com/questions/6792812/the-backspace-escape-character-b-in-c-unexpected-behavior – ctn Jun 21 '13 at 13:21
  • @suspectus I used an online gcc http://www.compileonline.com/compile_c_online.php – Yu Hao Jun 21 '13 at 13:21
  • 6
    @YuHao They don't do anything. They're just characters. But whatever you write those characters to might act a certain way when it receives those characters. e.g. if you write those characters to a file, the character will end up in the file. If you write it to a terminal/console, the terminal might erase a character when it sees the \b Some terminals would do that, some would not. – nos Jun 21 '13 at 13:22
  • In the string it ist just ASCII 8, and 13 CR. An old smart console could have interpreted these as commands (see alse Unix curls). In general `\r` might get interpreted as line break. _In fact you found the two most useless characters._ – Joop Eggen Jun 21 '13 at 13:25

6 Answers6

61

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.

ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
  • 7
    do you know how to configure mine so it doesnt ignore the bell(\a) ?? – mf_ Jun 21 '13 at 13:42
  • In Windows 10, when I opened it using the terminal, I heard the same sound as when Notepad asks if you want to save, and then you click out of the pop-up. When I opened the executable with two clicks or with PowerShell, I got the error message sound. – Schilive Sep 27 '22 at 01:15
12

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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 5
    Tthe standard doesn't say what the character codes are. `\b` will be `0x08` only if `0x08` corresponds to the backspace character in the execution character set (which is true in ASCII and Unicode and other common character sets). If your execution character set is EBCDIC, however, then `\b` will map to `\0x16` (if I can trust the character tables I'm finding online). – Adrian McCarthy Jun 21 '13 at 17:55
5

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.

Joni
  • 108,737
  • 14
  • 143
  • 193
1

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;
}

Yannis Dran
  • 1,479
  • 2
  • 18
  • 24
Moh K
  • 484
  • 1
  • 4
  • 17
  • 1
    Codes should be text , you can insert images for output which can't be posted here obviously as it won't display as it would on local machines – Morse Apr 26 '18 at 23:24
1

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. :)

Jacky Wang
  • 57
  • 2
1

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
sepideha
  • 1,669
  • 1
  • 11
  • 14