473,495 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

escapping \n, \t etc.

Hi All,

when you assign a string having \n, \t or \r etc.
like for eg str = "Hello\nWorld" ;

And do printf (str)

it will be printed as
Hello
World.

But, how can I print it as "Hello\nWorld"?

What is the best we to do it?

Thanks and regards,
Prasad.

Jan 13 '07 #1
7 5470
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.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jan 13 '07 #2
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.
Jan 13 '07 #3
Prasad wrote:
>
when you assign a string having \n, \t or \r etc.
like for eg str = "Hello\nWorld" ;

And do printf (str)

it will be printed as
Hello
World.

But, how can I print it as "Hello\nWorld"?

What is the best we to do it?
printf("Hello\\nWorld\n");
or
printf("%s\n", "Hello\\nWorld");
or
puts("Hellow\\nWorld");
or
printf("%s%c%s\n", "Hello", '\\', "nWorld");
or
think up some other possibilities.

--
"The most amazing achievement of the computer software industry
is its continuing cancellation of the steady and staggering
gains made by the computer hardware industry..." - Petroski

Jan 14 '07 #4
Prasad wrote:
Hi All,

when you assign a string having \n, \t or \r etc.
like for eg str = "Hello\nWorld" ;

And do printf (str)

it will be printed as
Hello
World.

But, how can I print it as "Hello\nWorld"?

What is the best we to do it?

Thanks and regards,
Prasad.
#include <stdio.h>
int main(void)
{
char str[] = "Hello\nWorld.";
char *s = str;
int c;
puts(str);
putchar('"');
while ((c = *s++) != 0)
if (c == '\n') {
putchar('\\');
putchar('n');
} else {
putchar(c);
}
putchar('"');
return 0;
}
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jan 14 '07 #5
Joe Wright wrote:
Prasad wrote:
Hi All,

when you assign a string having \n, \t or \r etc.
like for eg str = "Hello\nWorld" ;

And do printf (str)

it will be printed as
Hello
World.

But, how can I print it as "Hello\nWorld"?

What is the best we to do it?

Thanks and regards,
Prasad.
#include <stdio.h>
int main(void)
{
char str[] = "Hello\nWorld.";
char *s = str;
int c;
puts(str);
putchar('"');
while ((c = *s++) != 0)
if (c == '\n') {
putchar('\\');
Why do you need two backslashes here? Won't one do?
putchar('n');
} else {
putchar(c);
}
putchar('"');
return 0;
}
Jan 14 '07 #6
santosh said:
Joe Wright wrote:
<snip>
> putchar('\\');

Why do you need two backslashes here? Won't one do?
No.

See K&R2, page 8, or, if you prefer, 3.1.3.4 Character constants:

simple-escape-sequence: one of
\' \" \? \\
\a \b \f \n \r \t \v

'\' would be a single quote and an escaped-single-quote. This would mean
that the opening single quote was not matched. It is analogous to "\"
(which is an unterminated string).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 14 '07 #7
Keith Thompson <ks***@mib.orgwrote:
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.
I suppose that's most correct, but man printf on my system does indeed
discuss the various character escape sequences, hence my comment.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jan 14 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
305
by: Prasad | last post by:
Hi All, If the string is say, str = "Hello\nWorld" ; Now, when I call printf ( "%s", str) ; It is printed as Hello World. But I want it to be printed as "Hello\nWorld". Hence, I need to...
0
6991
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7196
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7373
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4897
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1405
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.