can I split a C string

K

KIRAN

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
 
B

badc0de4

KIRAN said:
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";
 
K

Keith Thompson

KIRAN said:
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.
 
P

Peter Nilsson

Keith said:
... 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.
 
K

KIRAN

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"
 
K

Keith Thompson

KIRAN said:
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.

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.

[...]
 
V

vippstar

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 \.
 
B

Bartc

Richard Heathfield said:
KIRAN said:


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.

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.
 
C

Coos Haak

Op Tue, 8 Jul 2008 02:20:18 -0700 (PDT) schreef (e-mail address removed):
Your current code is not valid. There's a space after \.

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

Coos Haak

Op Tue, 08 Jul 2008 17:48:16 +0000 schreef Richard Heathfield:
Coos Haak said:


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.
 
A

Antoninus Twink

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.
 
C

CBFalconer

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)
 
C

CBFalconer

Richard said:
Coos Haak said:

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.
 
N

Nick Keighley

[...] 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)

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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,260
Messages
2,571,039
Members
48,768
Latest member
first4landlord

Latest Threads

Top