I expect this simple line of code
printf(\"foo\\b\\tbar\\n\");
to replace \"o\" with \"\\t\" and to produce the following output
\t
is the tab character, and is doing exactly what you're anticipating based on the action of \b
- it goes to the next tab stop, then gets decremented, and then goes to the next tab stop (which is in this case the same tab stop, because of the \b
.
No, that's more or less what they're meant to do.
In C (and many other languages), you can insert hard-to-see/type characters using \
notation:
\a
is alert/bell\b
is backspace/rubout\n
is newline\r
is carriage return (return to left margin)\t
is tabYou can also specify the octal value of any character using \0
nnn, or the hexadecimal value of any character with \x
nn.
_
is octal 137, hex 5f, so it can also be typed \0137
or \x5f
, if your keyboard didn't have a _
key or something. This is more useful for control characters like NUL (\0
) and ESC (\033
)As someone posted (then deleted their answer before I could +1 it), there are also some less-frequently-used ones:
\f
is a form feed/new page (eject page from printer)\v
is a vertical tab (move down one line, on the same column)On screens, \f
usually works the same as \v
, but on some printers/teletypes, it will
go all the way to the next form/sheet of paper.
This behaviour is terminal-specific and specified by the terminal emulator you use (e.g. xterm
) and the semantics of terminal that it provides. The terminal behaviour has been very stable for the last 20 years, and you can reasonably rely on the semantics of \b
.
Backspace and tab both move the cursor position. Neither is truly a 'printable' character.
Your code says:
To get the output you expect, you need printf("foo\b \tbar")
. Note the extra 'space'. That says:
Most of the time it is inappropriate to use tabs and backspace for formatting your program output. Learn to use printf()
formatting specifiers. Rendering of tabs can vary drastically depending on how the output is viewed.
This little script shows one way to alter your terminal's tab rendering. Tested on Ubuntu + gnome-terminal:
#!/bin/bash
tabs -8
echo -e "\tnormal tabstop"
for x in `seq 2 10`; do
tabs $x
echo -e "\ttabstop=$x"
done
tabs -8
echo -e "\tnormal tabstop"
Also see man setterm
and regtabs
.
And if you redirect your output or just write to a file, tabs will quite commonly be displayed as fewer than the standard 8 chars, especially in "programming" editors and IDEs.
So in otherwords:
printf("%-8s%s", "foo", "bar"); /* this will ALWAYS output "foo bar" */
printf("foo\tbar"); /* who knows how this will be rendered */
IMHO, tabs in general are rarely appropriate for anything. An exception might be generating output for a program that requires tab-separated-value input files (similar to comma separated value).
Backspace '\b'
is a different story... it should never be used to create a text file since it will just make a text editor spit out garbage. But it does have many applications in writing interactive command line programs that cannot be accomplished with format strings alone. If you find yourself needing it a lot, check out "ncurses", which gives you much better control over where your output goes on the terminal screen. And typically, since it's 2011 and not 1995, a GUI is usually easier to deal with for highly interactive programs. But again, there are exceptions. Like writing a telnet server or console for a new scripting language.
The C standard (actually C99, I'm not up to date) says:
Alphabetic escape sequences representing nongraphic characters in the execution character set are intended to produce actions on display devices as follows:
\b
(backspace) Moves the active position to the previous position on the current line. [...]
\t
(horizontal tab) Moves the active position to the next horizontal tabulation position on the current line. [...]
Both just move the active position, neither are supposed to write any character on or over another character. To overwrite with a space you could try: puts("foo\b \tbar");
but note that on some display devices - say a daisy wheel printer - the o
will show the transparent space.