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

  2. herrcho

    Mike Wahler Guest

    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:


    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 Wahler, Sep 25, 2003
    1. Advertisements

  3. I just wonder if an implementation is at all required to store a string
    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

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

    Richard Bos Guest

    An implementation is required to do nothing at all, as long as the
    effect is the same.
    Yup. And is allowed to be. In most cases this optimisation won't be
    worth the trouble, but it's legal, all right.

    Richard Bos, Sep 25, 2003
  5. herrcho

    pete Guest

    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, Sep 25, 2003
  6. herrcho

    Jack Klein Guest

    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.
    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
    Jack Klein, Sep 26, 2003
  7. herrcho

    Mike Wahler Guest

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

    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 Wahler, Sep 26, 2003
  8. herrcho

    Jack Klein Guest

    Jack Klein, Sep 26, 2003
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.