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 TAB
s in strings would suffer greatly in readability, but is there a technical reason to use \t
other than convention?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With