470,831 Members | 1,689 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,831 developers. It's quick & easy.

can I split a C string

Hi all,
can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?
Any help or link to standard regarding the above doubt is
aprreciated...
Regards,
Kiran
Jul 7 '08 #1
23 2643
KIRAN wrote:
Hi all,
can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?
Any help or link to standard regarding the above doubt is
aprreciated...
Regards,
Kiran
You can use a backslash to split any line in a C program.
1 Be careful that there are no invisible white space
characters between the backslash and the following newline.
2 The conventional way of splitting string literals is simply
to enclose the parts in quotes:

char * p = "Hello "
"World\n\r";

The backslash is mostly just used for lengthy macros.

--
pete
Jul 7 '08 #2
KIRAN wrote:
Hi all,
can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?
Any help or link to standard regarding the above doubt is
aprreciated...
Yes, you can do it (assuming the \ immediately precedes the
newline ending the first line). The relevant references in the
Standard are 5.1.1.2p2 (line splicing occurs in translation
phase 2) and 6.4.5p4 (string literals are recognized in phase 6).

In the Bad Old Days this was often the only way to write
very long string literals without using very long source lines.
But the 1989 ANSI Standard codified a better solution: String
literals that are adjacent in the source, with nothing but white
space and comments between them, are concatenated (5.1.1.2p6).
So you could write

char *p = "Hello "
" World\n\r";

or even

char *p = "Hell" /* sorry, bad word */
"o" /* ah, that's better */
/* leave extra spaces, just in case: */ " "
/* and now, the recipient of our greeting: */
"W" /* Gimme a wuh! */
"o" /* Gimme an oh! */
"r" /* Gimme an ahh! */
"l" /* Gimme an ell! */
"d" /* Gimme a dee! */
"\n" /* end of line */ "\r" /* just for luck */
;

with the same effect.

--
Er*********@sun.com
Jul 7 '08 #3
KIRAN wrote:
can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?
Any help or link to standard regarding the above doubt is
aprreciated...
I think you can do that.
But you'll have 3 spaces between Hello and World.

Maybe it's better if you do it like this instead:

char * p = "Hello "
"World\r\n";
Jul 7 '08 #4
KIRAN <ki*****@gmail.comwrites:
can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?
In addition to the answers you've gotten (yes, you can, but there's a
much better way), why does your string end in "\n\r"? If that's meant
to be a Windows-style line ending, then (a) it's "\r\n", not "\n\r",
and (b) it's rarely necessary to store the "\r" in memory, since "\n"
will be automatically translated as needed when written to a text
stream.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 7 '08 #5
Keith Thompson wrote:
... If that's meant to be a Windows-style line ending,
then (a) it's "\r\n", not "\n\r",
Not necessarily. Old Macs used CR for end of line. Some
implementations allowed you to swap the more typical
values of \r and \n.

--
Peter
Jul 7 '08 #6
On Jul 7, 10:13*pm, Keith Thompson <ks...@mib.orgwrote:
KIRAN <kira...@gmail.comwrites:
can i split a C string like this?
char * p = "Hello \
* World\n\r";
is this according to standard?

In addition to the answers you've gotten (yes, you can, but there's a
much better way), why does your string end in "\n\r"? *
Actually I am writing portable code that runs on windows & ARM9 .
my print function will print

1. (WINDOWS)the string on console window(here "\r" is not
required(correct me if am wrong)

2. (ARM) the string on hyperterminal(UART)
>If that's meant
to be a Windows-style line ending, then (a) it's "\r\n", not "\n\r",
and
Why?

(b) it's rarely necessary to store the "\r" in memory, since "\n"
will be automatically translated as needed when written to a text
stream.

--
Keith Thompson (The_Other_Keith) ks...@mib.org *<http://www.ghoti.net/~kst>
Nokia
"We must do something. *This is something. *Therefore, we must do this."
* * -- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 8 '08 #7
KIRAN <ki*****@gmail.comwrites:
On Jul 7, 10:13*pm, Keith Thompson <ks...@mib.orgwrote:
>KIRAN <kira...@gmail.comwrites:
can i split a C string like this?
char * p = "Hello \
* World\n\r";
is this according to standard?

In addition to the answers you've gotten (yes, you can, but there's a
much better way), why does your string end in "\n\r"? *

Actually I am writing portable code that runs on windows & ARM9 .
my print function will print

1. (WINDOWS)the string on console window(here "\r" is not
required(correct me if am wrong)
It's not required if you're writing to a stream that was opened in
text mode. I'm not sure what will happen if you write the '\r'
anyway. If you write "\n\r", it's likely that you'll get an extra
'\r' at the beginning of each line; this may or may not be harmless.
2. (ARM) the string on hyperterminal(UART)
I don't know enough about UARTs to know what will happen in that case.
>>If that's meant
to be a Windows-style line ending, then (a) it's "\r\n", not "\n\r",
and

Why?
I don't know; it's just the way line endings are usually represented
on Windows. I don't know what "\n\r" (LF CR) in a Windows text file
means; for all I know it might be valid. Try a Windows newsgroup if
you want more information.

[...]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 8 '08 #8
KIRAN said:
On Jul 7, 10:13 pm, Keith Thompson <ks...@mib.orgwrote:
>KIRAN <kira...@gmail.comwrites:
can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?

In addition to the answers you've gotten (yes, you can, but there's a
much better way), why does your string end in "\n\r"?

Actually I am writing portable code that runs on windows & ARM9 .
my print function will print

1. (WINDOWS)the string on console window(here "\r" is not
required(correct me if am wrong)

2. (ARM) the string on hyperterminal(UART)
>>If that's meant
to be a Windows-style line ending, then (a) it's "\r\n", not "\n\r",
and

Why?
Because there was a teletype device in the 1960s on which moving the
carriage (the thing that carried the print head) from the right edge of
the paper to the left edge took twice as long as it took for a character
to arrive over the wire. To avoid dropping characters during the return of
the carriage from right to left, the designers split the task into two -
returning the carriage to the left edge of the paper, and feeding the
paper up a line - so that it would require two input characters to do this
one task, and thus the carriage would have time to do its thing without
any characters being dropped. (Essentially, the linefeed started life as a
padding character.)

CP/M uncritically adopted the CR/LF convention, wasting one byte per line
of text file. Recognising this, the Unix guys dropped the CR, whereas the
Mac folks dropped the LF. 86-DOS (QDOS) inherited the pair from CP/M, and
then was rebadged as MS-DOS, which was later rebadged as Windows. So even
in 2008, Windows users are stuck with this two-character newline encoding
several decades after the obsolescence of the hardware whose shortcomings
it was introduced to work around.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 8 '08 #9
On Jul 7, 6:24 pm, KIRAN <kira...@gmail.comwrote:
Hi all,
can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?
Your current code is not valid. There's a space after \.
Jul 8 '08 #10
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:NP*********************@bt.com...
KIRAN said:
>On Jul 7, 10:13 pm, Keith Thompson <ks...@mib.orgwrote:
>>KIRAN <kira...@gmail.comwrites:
can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?

In addition to the answers you've gotten (yes, you can, but there's a
much better way), why does your string end in "\n\r"?

Actually I am writing portable code that runs on windows & ARM9 .
my print function will print

1. (WINDOWS)the string on console window(here "\r" is not
required(correct me if am wrong)

2. (ARM) the string on hyperterminal(UART)
>>>If that's meant
to be a Windows-style line ending, then (a) it's "\r\n", not "\n\r",
and

Why?

Because there was a teletype device in the 1960s on which moving the
carriage (the thing that carried the print head) from the right edge of
the paper to the left edge took twice as long as it took for a character
to arrive over the wire. To avoid dropping characters during the return of
the carriage from right to left, the designers split the task into two -
returning the carriage to the left edge of the paper, and feeding the
paper up a line - so that it would require two input characters to do this
one task, and thus the carriage would have time to do its thing without
any characters being dropped. (Essentially, the linefeed started life as a
padding character.)
If you're talking about the ASR33, this I think /required/ the two
characters, CR to move the head to the left, and LF to advance a line. They
were two different operations.

For convenience, whenever the user pressed Return, it was echoed back as CR
LF.
>
CP/M uncritically adopted the CR/LF convention, wasting one byte per line
of text file. Recognising this, the Unix guys dropped the CR, whereas the
Mac folks dropped the LF. 86-DOS (QDOS) inherited the pair from CP/M, and
then was rebadged as MS-DOS, which was later rebadged as Windows. So even
in 2008, Windows users are stuck with this two-character newline encoding
several decades after the obsolescence of the hardware whose shortcomings
it was introduced to work around.
I think Windows itself didn't help by requiring both CR and LF to be present
when sending text to the console, just like the teletype, otherwise lines
would either overwrite themselves (LF missing), or newlines would start
halfway across the screen (CR missing). So it was advisable to keep the CRLF
ending in a text file.
>On Jul 7, 10:13 pm, Keith Thompson <ks...@mib.orgwrote:
>>>If that's meant
to be a Windows-style line ending, then (a) it's "\r\n", not "\n\r",
In text mode these will result in CR CR LF or CR LF CR, even more waste. And
if sent to a file, a lot of confusion for programs that expect specific line
endings.

--
Bartc
Jul 8 '08 #11
Bartc said:
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:NP*********************@bt.com...
<snip>
>To avoid dropping characters during the return
of the carriage from right to left, the designers split the task into
two - returning the carriage to the left edge of the paper, and feeding
the paper up a line - so that it would require two input characters to
do this one task, and thus the carriage would have time to do its thing
without any characters being dropped. (Essentially, the linefeed started
life as a padding character.)

If you're talking about the ASR33, this I think /required/ the two
characters, CR to move the head to the left, and LF to advance a line.
They were two different operations.
Yes, that's a reasonable re-phrasing of part of what I said.

<snip>
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 8 '08 #12
Op Tue, 8 Jul 2008 02:20:18 -0700 (PDT) schreef vi******@gmail.com:
On Jul 7, 6:24 pm, KIRAN <kira...@gmail.comwrote:
>Hi all,
can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?
Your current code is not valid. There's a space after \.
Only when you use (of view it with) google groups ;-)
--
Coos

Jul 8 '08 #13
Coos Haak said:
Op Tue, 8 Jul 2008 02:20:18 -0700 (PDT) schreef vi******@gmail.com:
>On Jul 7, 6:24 pm, KIRAN <kira...@gmail.comwrote:
>>Hi all,
can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?
Your current code is not valid. There's a space after \.

Only when you use (of view it with) google groups ;-)
No, the article source has the space there.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 8 '08 #14
Op Tue, 08 Jul 2008 17:48:16 +0000 schreef Richard Heathfield:
Coos Haak said:
>Op Tue, 8 Jul 2008 02:20:18 -0700 (PDT) schreef vi******@gmail.com:
>>On Jul 7, 6:24 pm, KIRAN <kira...@gmail.comwrote:
Hi all,
can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?
Your current code is not valid. There's a space after \.

Only when you use (of view it with) google groups ;-)

No, the article source has the space there.
Not when I see it in my news reader. The space I saw was in the posting in
google groups. I assumed from his account @gmail.com that the OP posted
there.

--
Coos

Jul 8 '08 #15
Coos Haak said:
Op Tue, 08 Jul 2008 17:48:16 +0000 schreef Richard Heathfield:
>Coos Haak said:
>>Op Tue, 8 Jul 2008 02:20:18 -0700 (PDT) schreef vi******@gmail.com:

On Jul 7, 6:24 pm, KIRAN <kira...@gmail.comwrote:
Hi all,
can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?
Your current code is not valid. There's a space after \.

Only when you use (of view it with) google groups ;-)

No, the article source has the space there.

Not when I see it in my news reader. The space I saw was in the posting
in google groups. I assumed from his account @gmail.com that the OP
posted there.
Here's a hexdump which I knocked together to prove my point.

48 69 20 61 6C 6C 2C 0A 63 61 6E 20 69 20 73 70
6C 69 74 20 61 20 43 20 73 74 72 69 6E 67 20 6C
69 6B 65 20 74 68 69 73 3F 0A 63 68 61 72 20 2A
20 70 20 3D 20 22 48 65 6C 6C 6F 20 5C 0A 20 20
57 6F 72 6C 64 5C 6E 5C 72 22 3B 0A

It appears to prove yours instead! :-)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 8 '08 #16
On 8 Jul 2008 at 8:21, Richard Heathfield wrote:
So even in 2008, Windows users are stuck with this two-character
newline encoding several decades after the obsolescence of the
hardware whose shortcomings it was introduced to work around.
Not unlike users of your beloved C89, who are stuck with the unsafe
gets() function several decades after its security flaws became clear to
the world. Of course, you're not a hypocrite - the two situations are
quite, quite different. Not.

Jul 8 '08 #17
vi******@gmail.com wrote:
KIRAN <kira...@gmail.comwrote:
>>
can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?

Your current code is not valid. There's a space after \.
Your newsreader is faulty. There is no such space. However most
output from the google newsreader is faulty in one way or another.

Get yourself a real newsreader (e.g. Thunderbird, from mozilla.com)
and a real newsserver. The following list indicates several
possibilities for the newsserver.

Some free news servers. I use motzarella, teranews and gmane.
<http://www.teranews.com (1 time charge) (free)
<http://news.aioe.org (free)
<http://dotsrc.org (free)
<http://www.x-privat.org/international.php (free)
<http://motzarella.org/?language=en (free)
<http://gmane.org/ (mail-lists via news) (free)
<http://www.newsfeeds.com/signup.htm (pay)
<http://www.individual.net/ (low pay)

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Jul 8 '08 #18
Richard Heathfield wrote:
Coos Haak said:
>schreef vi******@gmail.com:
>>KIRAN <kira...@gmail.comwrote:

can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?

Your current code is not valid. There's a space after \.

Only when you use (of view it with) google groups ;-)

No, the article source has the space there.
You are exposing some fault in your reader system (or in mine). I
find no extra blank in the original message. Don't look at the
quoted line.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

Jul 8 '08 #19
Richard Heathfield wrote:
Coos Haak said:
>schreef vi******@gmail.com:
>>KIRAN <kira...@gmail.comwrote:

can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?

Your current code is not valid. There's a space after \.

Only when you use (of view it with) google groups ;-)

No, the article source has the space there.
You are exposing some fault in your reader system (or in mine). I
find no extra blank in the original message. Don't look at the
quoted line.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

Jul 8 '08 #20
Richard Heathfield wrote:
Bartc said:
>"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:NP*********************@bt.com...

<snip>
>>To avoid dropping characters during the return
of the carriage from right to left, the designers split the task into
two - returning the carriage to the left edge of the paper, and feeding
the paper up a line - so that it would require two input characters to
do this one task, and thus the carriage would have time to do its thing
without any characters being dropped. (Essentially, the linefeed started
life as a padding character.)
If you're talking about the ASR33, this I think /required/ the two
characters, CR to move the head to the left, and LF to advance a line.
They were two different operations.

Yes, that's a reasonable re-phrasing of part of what I said.

<snip>

Just to be pedantic, typewriter technology of the time had 'carriage' to
be that piece of the machinery which held the platen (and paper) back
and forth behind the fixed print mechanism. Later designs have the
platen/paper system fixed and a print head moving back and forth over
the paper. This print mechanism is called a 'carrier'.

Some of the first terminals were based on the IBM Model B which had a
platen carriage. Later terminals were ASR-33 or IBM Selectric based
which had a print head 'carrier'.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jul 8 '08 #21
On Jul 8, 2:20*pm, vipps...@gmail.com wrote:
On Jul 7, 6:24 pm, KIRAN <kira...@gmail.comwrote:Hi all,
can i split a C string like this?
char * p = "Hello \
* World\n\r";
is this according to standard?

Your current code is not valid. There's a space after \.
Is having space in string invalid?
Jul 9 '08 #22
ki*********@gmail.com said:
On Jul 8, 2:20 pm, vipps...@gmail.com wrote:
>On Jul 7, 6:24 pm, KIRAN <kira...@gmail.comwrote:Hi all,
can i split a C string like this?
char * p = "Hello \
World\n\r";
is this according to standard?

Your current code is not valid. There's a space after \.

Is having space in string invalid?
If you wish to split a logical line into two physical lines, you can do so
by placing a backslash and a newline into the logical line. You can't get
the same effect by placing a backslash, space, and newline.

As it turns out, the claim of a space appears to be mistaken.

But just so that you're clear about it, let's pretend that $ is your
system's way of representing a space (because $ is visible which makes it
a lot easier to talk about). You can split a C string like this:

char$*p$=$"Hello$\
$$$world\n\r";

but not like this:

char$*p$=$"Hello$\$
$$$world\n\r";

The \ must be the last thing on the line, you see (apart from the newline
itself, obviously).

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 9 '08 #23
On 8 Jul, 07:45, KIRAN <kira...@gmail.comwrote:
On Jul 7, 10:13*pm, Keith Thompson <ks...@mib.orgwrote:
KIRAN <kira...@gmail.comwrites:
<snip>
[...] why does your string end in "\n\r"? *

Actually I am writing portable code that runs on windows & ARM9 .
my print function will print

1. (WINDOWS)the string on console window(here "\r" is not
required(correct me if am wrong)
you are wrong.

2. (ARM) the string on hyperterminal(UART)
If that's meant
to be a Windows-style line ending, then (a) it's "\r\n", not "\n\r",
The idea is that "\n" sent to a stream that was opened in text mode
generates
the correct line ending sequence. That is \r\n for windows \n for Unix
and \r
for the Mac. I've no idea waht ARMs do. If you open the stream in
binary mode
the charcaters are untranslated. So if your ARM library is working
correctly
you *don't need* the \r.

long long ago I worked on a system where \r\n (and even \r\r\n) line
ending
was required. We used text streams and output the extra characters (I
think
the i/o system behaved in a Unix-like way). Now I'd probably open the
streams as binary and do the line endings "by hand".
--
Nick Keighley
Jul 9 '08 #24

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Stu Cazzo | last post: by
11 posts views Thread by Carlos Ribeiro | last post: by
6 posts views Thread by Senthil | last post: by
4 posts views Thread by Itzik | last post: by
4 posts views Thread by Crirus | last post: by
5 posts views Thread by kurt sune | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.