can I split a C string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • KIRAN

    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
  • pete

    #2
    Re: can I split a C string

    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

    Comment

    • Eric Sosman

      #3
      Re: can I split a C string

      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.

      --
      Eric.Sosman@sun .com

      Comment

      • badc0de4@gmail.com

        #4
        Re: can I split a C string

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

        Comment

        • Keith Thompson

          #5
          Re: can I split a C string

          KIRAN <kiraank@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_Keit h) kst-u@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"

          Comment

          • Peter Nilsson

            #6
            Re: can I split a C string

            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

            Comment

            • KIRAN

              #7
              Re: can I split a C string

              On Jul 7, 10:13 pm, Keith Thompson <ks...@mib.orgw rote:
              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(correc t me if am wrong)

              2. (ARM) the string on hyperterminal(U ART)
              >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_Keit h) 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"

              Comment

              • Keith Thompson

                #8
                Re: can I split a C string

                KIRAN <kiraank@gmail. comwrites:
                On Jul 7, 10:13 pm, Keith Thompson <ks...@mib.orgw rote:
                >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(correc t 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(U ART)
                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_Keit h) kst-u@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"

                Comment

                • Richard Heathfield

                  #9
                  Re: can I split a C string

                  KIRAN said:
                  On Jul 7, 10:13 pm, Keith Thompson <ks...@mib.orgw rote:
                  >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(correc t me if am wrong)
                  >
                  2. (ARM) the string on hyperterminal(U ART)
                  >
                  >>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

                  Comment

                  • vippstar@gmail.com

                    #10
                    Re: can I split a C string

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

                    Comment

                    • Bartc

                      #11
                      Re: can I split a C string

                      "Richard Heathfield" <rjh@see.sig.in validwrote in message
                      news:NPydncrGj7 D_v-7VRVnyugA@bt.co m...
                      KIRAN said:
                      >
                      >On Jul 7, 10:13 pm, Keith Thompson <ks...@mib.orgw rote:
                      >>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(corre ct me if am wrong)
                      >>
                      >2. (ARM) the string on hyperterminal(U ART)
                      >>
                      >>>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.orgw rote:
                      >>>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


                      Comment

                      • Richard Heathfield

                        #12
                        Re: can I split a C string

                        Bartc said:
                        "Richard Heathfield" <rjh@see.sig.in validwrote in message
                        news:NPydncrGj7 D_v-7VRVnyugA@bt.co m...
                        <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

                        Comment

                        • Coos Haak

                          #13
                          Re: can I split a C string

                          Op Tue, 8 Jul 2008 02:20:18 -0700 (PDT) schreef vippstar@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

                          Comment

                          • Richard Heathfield

                            #14
                            Re: can I split a C string

                            Coos Haak said:
                            Op Tue, 8 Jul 2008 02:20:18 -0700 (PDT) schreef vippstar@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

                            Comment

                            • Coos Haak

                              #15
                              Re: can I split a C string

                              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 vippstar@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

                              Comment

                              Working...