Christopher Benson-Manica <at***@ukato.freeshell.orgwrites:
Prasad <pr************@gmail.comwrote:
>when you assign a string having \n, \t or \r etc.
like for eg str = "Hello\nWorld" ;
>But, how can I print it as "Hello\nWorld"?
Escape the backslash:
const char *str = "Hello\\nWorld";
printf()'s documentation is your friend.
printf()'s documentation won't help here. The fact that "\n" contains
a newline character, while "\\n" contains a backslash followed by 'n',
is determined by the syntax of string literals.
Incidentally, it wasn't clear to me whether the OP wanted to do this
conversion in his source, or at run time. For example, it could be
useful to have a function, that, given the string "Hello\nWorld",
would print
Hello\nWorld
rather than
Hello
World
This requires scanning the string and replacing each newline character
with the two-character sequence of a backslash and an 'n'. There's no
function in the standard library that will automatically do this for
you, but it's easy enough to write the code yourself.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.