Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use "\t" instead of "<press TAB>" for strings? [duplicate]

I just tried compiling some C++ code that used the literal Tab character (as in I pressed the TAB key on my keyboard) inside a string. And, to my surprise, it compiled fine. In retrospect I guess that makes sense, since it's a character just like any other.

cout << "Tab: [TAB]";

Before now I've always used \t to define tabs in strings.

cout << "Tab: [\t]";

Obviously code using literal TABs in strings would suffer greatly in readability, but is there a technical reason to use \t other than convention?

like image 542
TheSchwa Avatar asked Feb 22 '16 22:02

TheSchwa


1 Answers

but is there a technical reason to use \t other than convention?

Sure there are technical reasonings. Using

cout << "Tab: [\t]";

would work independently of the target systems actual character encoding.

The source file could use a different encoding as the target system does, e.g. UTF8 for source, but EBCDIC at the target system.
'\t' will always be translated to the correct character code though, thus the code is portable.


Also as mentioned in merlin2011's comment many IDE's will just replace TAB with a specific number of spaces.

like image 78
πάντα ῥεῖ Avatar answered Oct 12 '22 17:10

πάντα ῥεῖ