String, String literal ? Could anyone explain to me ?

Discussion in 'C Programming' started by herrcho, Sep 25, 2003.

  1. herrcho

    herrcho Guest

    i'm in the course of learning C, and found these two words "string,
    string literal" confusing me..

    I'd like to know the difference between them.. Thank you
    herrcho, Sep 25, 2003
    #1
    1. Advertising

  2. herrcho

    Mike Wahler Guest

    "herrcho" <> wrote in message
    news:bktpj5$8e$...
    > i'm in the course of learning C, and found these two words "string,
    > string literal" confusing me..
    >
    > I'd like to know the difference between them.. Thank you


    In C, a 'string' is an array of characters, the
    last of which has a value of zero. A string can
    have automatic, static, or allocated duration.
    It can be defined to be modifiable or not
    (see 'const').

    A 'string literal' can appear in source code by
    enclosing a character sequence in double quotes as in:

    "Hello"

    A 'string literal' represents a nonmodifiable string
    in your program's memory space. (It occupies one more
    character than those expressed between the quotes --
    an implied terminator character ('\0') ). So the
    string literal "Hello" occupies six bytes.

    char s[20]; /* an array of twenty uninitalized characters */
    strcpy(s, "Hello"); /* copy the characters of the string
    literal to the array 's' ('strcpy()'
    automatically adds the '\0' terminator) */

    /* now the array 's' contains a string. (Note that if
    a terminator character ('\0') is not an element of
    the array, then it's not a string */


    -Mike
    Mike Wahler, Sep 25, 2003
    #2
    1. Advertising

  3. "Mike Wahler" <> schrieb im Newsbeitrag
    news:Mgvcb.2768$...
    >
    > "herrcho" <> wrote in message
    > news:bktpj5$8e$...
    > > i'm in the course of learning C, and found these two words "string,

    [....]

    > A 'string literal' can appear in source code by
    > enclosing a character sequence in double quotes as in:
    >
    > "Hello"
    >
    > A 'string literal' represents a nonmodifiable string
    > in your program's memory space. (It occupies one more
    > character than those expressed between the quotes --
    > an implied terminator character ('\0') ). So the
    > string literal "Hello" occupies six bytes.


    I just wonder if an implementation is at all required to store a string
    literal.
    The statement
    char foo[] = "Hello";
    as well as
    char *bar = "Hello";
    char baz = bar[1];

    for example can be executed using assembly instructions with immidiate
    operands

    Of course this would be a pretty strange implementation. The question is
    just "does the standard _require_ string literals to be stored somewhere?"
    Robert
    Robert Stankowic, Sep 25, 2003
    #3
  4. herrcho

    Richard Bos Guest

    "Robert Stankowic" <> wrote:

    > I just wonder if an implementation is at all required to store a string
    > literal.


    An implementation is required to do nothing at all, as long as the
    effect is the same.

    > The statement
    > char foo[] = "Hello";
    > as well as
    > char *bar = "Hello";
    > char baz = bar[1];
    >
    > for example can be executed using assembly instructions with immidiate
    > operands


    Yup. And is allowed to be. In most cases this optimisation won't be
    worth the trouble, but it's legal, all right.

    Richard
    Richard Bos, Sep 25, 2003
    #4
  5. herrcho

    pete Guest

    Robert Stankowic wrote:
    >
    > "Mike Wahler" <> schrieb im Newsbeitrag
    > news:Mgvcb.2768$...
    > >
    > > "herrcho" <> wrote in message
    > > news:bktpj5$8e$...
    > > > i'm in the course of learning C, and found these two words "string,

    > [....]
    >
    > > A 'string literal' can appear in source code by
    > > enclosing a character sequence in double quotes as in:
    > >
    > > "Hello"
    > >
    > > A 'string literal' represents a nonmodifiable string
    > > in your program's memory space. (It occupies one more
    > > character than those expressed between the quotes --
    > > an implied terminator character ('\0') ). So the
    > > string literal "Hello" occupies six bytes.

    >
    > I just wonder if an implementation is at all required to store a string
    > literal.
    > The statement
    > char foo[] = "Hello";
    > as well as
    > char *bar = "Hello";


    When initializing in an array of char declaration, as above,
    the string literal's presence in the code,
    does not imply that there is another object besides foo.

    However, the string literal is
    "the name of an anonymous object" (how's that for an oxymoron?)
    in the pointer assignment.

    --
    pete
    pete, Sep 25, 2003
    #5
  6. herrcho

    Jack Klein Guest

    On Thu, 25 Sep 2003 05:44:12 GMT, "Mike Wahler"
    <> wrote in comp.lang.c:

    >
    > "herrcho" <> wrote in message
    > news:bktpj5$8e$...
    > > i'm in the course of learning C, and found these two words "string,
    > > string literal" confusing me..
    > >
    > > I'd like to know the difference between them.. Thank you

    >
    > In C, a 'string' is an array of characters, the
    > last of which has a value of zero. A string can
    > have automatic, static, or allocated duration.
    > It can be defined to be modifiable or not
    > (see 'const').


    I have to nit-pick this one. Consider:

    char ca [20] = "Hello";
    ca [19] = '!';

    Now ca is an array of characters, the last of which most specifically
    does not have a value of 0. Yet ca is a string.

    From 7.1.1 of C99:

    "A string is a contiguous sequence of characters terminated by and
    including the first null character."

    An array of characters may contain a string, as in my example, and not
    meet the definition of a string you posted.

    >
    > A 'string literal' can appear in source code by
    > enclosing a character sequence in double quotes as in:
    >
    > "Hello"
    >
    > A 'string literal' represents a nonmodifiable string
    > in your program's memory space. (It occupies one more
    > character than those expressed between the quotes --
    > an implied terminator character ('\0') ). So the
    > string literal "Hello" occupies six bytes.
    >
    > char s[20]; /* an array of twenty uninitalized characters */
    > strcpy(s, "Hello"); /* copy the characters of the string
    > literal to the array 's' ('strcpy()'
    > automatically adds the '\0' terminator) */
    >
    > /* now the array 's' contains a string. (Note that if
    > a terminator character ('\0') is not an element of
    > the array, then it's not a string */


    BTW, usenet RFCs specify that a signature be separated from the body
    of a message by a line consisting of the three characters "-- ".

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Sep 26, 2003
    #6
  7. herrcho

    Mike Wahler Guest

    "Jack Klein" <> wrote in message
    news:...
    > On Thu, 25 Sep 2003 05:44:12 GMT, "Mike Wahler"
    > <> wrote in comp.lang.c:
    >
    > >
    > > "herrcho" <> wrote in message
    > > news:bktpj5$8e$...
    > > > i'm in the course of learning C, and found these two words "string,
    > > > string literal" confusing me..
    > > >
    > > > I'd like to know the difference between them.. Thank you

    > >
    > > In C, a 'string' is an array of characters, the
    > > last of which has a value of zero. A string can
    > > have automatic, static, or allocated duration.
    > > It can be defined to be modifiable or not
    > > (see 'const').

    >
    > I have to nit-pick this one. Consider:
    >
    > char ca [20] = "Hello";
    > ca [19] = '!';
    >
    > Now ca is an array of characters, the last of which most specifically
    > does not have a value of 0. Yet ca is a string.
    >
    > From 7.1.1 of C99:
    >
    > "A string is a contiguous sequence of characters terminated by and
    > including the first null character."
    >
    > An array of characters may contain a string, as in my example, and not
    > meet the definition of a string you posted.


    Yes, that's what I meant. You're just picking on me
    for fun now, huh? Just kidding. :) My description
    was indeed sloppy. Thanks for 'cleaning it up'.


    [snip]

    > BTW, usenet RFCs specify that a signature be separated from the body
    > of a message by a line consisting of the three characters "-- ".


    IMO it's not a 'signature' in the sense you're using. It's
    just part of my message body. Sometimes I put "Love," before
    it, but I don't know you folks that well. :)

    -Mike
    Mike Wahler, Sep 26, 2003
    #7
  8. herrcho

    Jack Klein Guest

    On Fri, 26 Sep 2003 06:26:30 GMT, "Mike Wahler"
    <> wrote in comp.lang.c:


    > > BTW, usenet RFCs specify that a signature be separated from the body
    > > of a message by a line consisting of the three characters "-- ".

    >
    > IMO it's not a 'signature' in the sense you're using. It's
    > just part of my message body. Sometimes I put "Love," before
    > it, but I don't know you folks that well. :)
    >
    > -Mike


    Now you've gone and broken my heart! :(

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Sep 26, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    487
    Henry S. Thompson
    Nov 28, 2005
  2. =?Utf-8?B?QW5kcmV3?=

    Could anyone explain this Yahoo! source code?

    =?Utf-8?B?QW5kcmV3?=, May 4, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    812
    Karl Seguin [MVP]
    May 4, 2006
  3. C++fan
    Replies:
    14
    Views:
    721
    C++fan
    Jan 5, 2004
  4. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    355
    Anonieko Ramos
    Sep 27, 2004
  5. Replies:
    9
    Views:
    106
    -berlin.de
    Mar 18, 2007
Loading...

Share This Page