473,387 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 5468
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
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
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.