Need Clarification

Discussion in 'C Programming' started by Newbie, Jul 11, 2008.

  1. Newbie

    Newbie Guest

    Can u tell me more abt how did the array a[20] got converted into string
    a...!!

    To this question some-one answered :

    No, it won't convert into string object.
    Here the representation of string is as character arrays.

    Ok.This part is fine.

    It can be classifieds depends on the character used to indicate the end
    of the string. "In C it is ASCII-Z representation and end with ASCII
    value 0"

    Is this comment correct ? I did not find anything similar in the C
    standard or am i missing something ?

    What about systems that don't use the ASCII character set (say if they
    use EBCDIC instead ?)
    Newbie, Jul 11, 2008
    #1
    1. Advertising

  2. In article <g581t2$170$>, Newbie <> wrote:
    >Can u tell me more abt how did the array a[20] got converted into string
    >a...!!


    >To this question some-one answered :


    >No, it won't convert into string object.
    >Here the representation of string is as character arrays.


    >Ok.This part is fine.


    >It can be classifieds depends on the character used to indicate the end
    >of the string. "In C it is ASCII-Z representation and end with ASCII
    >value 0"


    >Is this comment correct ? I did not find anything similar in the C
    >standard or am i missing something ?


    >What about systems that don't use the ASCII character set (say if they
    >use EBCDIC instead ?)


    No matter which character set you are using, C strings are stored in
    C array of char, and the string may be smaller than the array that it
    is stored in. The end of the string (as opposed to the end of the
    char array) is the first array entry with numeric value 0; if that
    numeric 0 happens to be right at the beginning of the array, then the
    string is commonly referred to as being "empty" -- but note that
    an "empty" string actually needs to have one value stored in it, the
    numeric 0 that marks the end of the string.

    --
    "I will speculate that [...] applications [...] could actually see a
    performance boost for most users by going dual-core [...] because it
    is running the adware and spyware that [...] are otherwise slowing
    down the single CPU that user has today" -- Herb Sutter
    Walter Roberson, Jul 11, 2008
    #2
    1. Advertising

  3. Newbie <> writes:

    > Can u tell me more abt how did the array a[20] got converted into
    > string a...!!
    >
    > To this question some-one answered :
    >
    > No, it won't convert into string object.
    > Here the representation of string is as character arrays.
    >
    > Ok.This part is fine.
    >
    > It can be classifieds depends on the character used to indicate the
    > end of the string. "In C it is ASCII-Z representation and end with
    > ASCII value 0"
    >
    > Is this comment correct ? I did not find anything similar in the C
    > standard or am i missing something ?
    >
    > What about systems that don't use the ASCII character set (say if they
    > use EBCDIC instead ?)


    This seems to be the middle of a conversation but I have no context
    for it so all I can say is:

    A character array can be said to hold a C string if it has a zero byte
    somewhere in it. All C strings are zero terminated. A character
    array without a zero byte is just an array of characters. The
    character set is not important -- only that the byte that marks the
    end be arithmetically zero. All sane character sets ensure that zero
    can be used for this purpose.

    In fact, an array can hold more than one string, but exactly how many
    is a matter of definition. This array:

    char my_strings[] = { 'a', 'b', 'c', '\0', 'd', '\0' };

    has at least two strings in it: the C expressions my_strings and
    my_strings + 4 both point to the start of valid strings (some people
    choose to say that my_strings *is* a string -- feel free to do that in
    casual conversation, but technically it is a pointer to the start of a
    string).

    my_strings + 1, my_strings + 2, and so on also point to (the start of)
    strings, so that array can be said to hold 5 strings that would
    compare as being different. It has two zero length strings
    (my_strings + 3 and my_strings + 5) but they compare equal.

    Hope that helps.

    --
    Ben.
    Ben Bacarisse, Jul 11, 2008
    #3
  4. Newbie

    Newbie Guest

    Ben Bacarisse wrote:
    > Newbie <> writes:
    >
    >> Can u tell me more abt how did the array a[20] got converted into
    >> string a...!!
    >>
    >> To this question some-one answered :
    >>
    >> No, it won't convert into string object.
    >> Here the representation of string is as character arrays.
    >>
    >> Ok.This part is fine.
    >>
    >> It can be classifieds depends on the character used to indicate the
    >> end of the string. "In C it is ASCII-Z representation and end with
    >> ASCII value 0"
    >>
    >> Is this comment correct ? I did not find anything similar in the C
    >> standard or am i missing something ?
    >>
    >> What about systems that don't use the ASCII character set (say if they
    >> use EBCDIC instead ?)

    >
    > This seems to be the middle of a conversation but I have no context
    > for it so all I can say is:
    >
    > A character array can be said to hold a C string if it has a zero byte
    > somewhere in it. All C strings are zero terminated. A character
    > array without a zero byte is just an array of characters. The
    > character set is not important -- only that the byte that marks the
    > end be arithmetically zero. All sane character sets ensure that zero
    > can be used for this purpose.
    >
    > In fact, an array can hold more than one string, but exactly how many
    > is a matter of definition. This array:
    >
    > char my_strings[] = { 'a', 'b', 'c', '\0', 'd', '\0' };
    >
    > has at least two strings in it: the C expressions my_strings and
    > my_strings + 4 both point to the start of valid strings (some people
    > choose to say that my_strings *is* a string -- feel free to do that in
    > casual conversation, but technically it is a pointer to the start of a
    > string).
    >
    > my_strings + 1, my_strings + 2, and so on also point to (the start of)
    > strings, so that array can be said to hold 5 strings that would
    > compare as being different. It has two zero length strings
    > (my_strings + 3 and my_strings + 5) but they compare equal.
    >
    > Hope that helps.
    >

    Thanks You all.The only thing i wanted to know was whether it was right
    to say "In C it is ASCII-Z representation and end with ASCII value 0"

    My point was the All C guaranteed was that every string is NULL
    terminated.It does not require the implementation to use ASCII character
    set.

    Thanks again for the quick replies.
    Newbie, Jul 11, 2008
    #4
  5. Newbie <> writes:
    <snip>
    > Thanks You all.The only thing i wanted to know was whether it was
    > right to say "In C it is ASCII-Z representation and end with ASCII
    > value 0"


    It may be daft to labour the point (you seem happy with the answers)
    but the reason I did not answer this question is that I can't put any
    meaning to the words. I can't link the two clauses with "and" and the
    only meaning I can give to "ASCII-Z representation" (the number 90)
    makes not sense in this context.

    One often reads things like "ASCII value 0". But the word "ASCII" has
    no meaning here. ASCII value 0 is the value 0. It is a property of
    all encodings that "Froodle value 90" is 90. What 90 *means* in the
    Froodle character set is, of course, a matter for the Froodle
    standards committee. If they decide that the character 'A' is to be
    represented by the value 0, then it is not possible to use Froodle as
    the execution character set for a C implementation because it is the
    value 0 (regardless of any meaning it might have) that terminates
    strings in C, and C insists that strings be both zero terminated and
    be able to hold the character 'A'.

    Anyway, I am glad that you are content. If this is more than you
    want to know, I apologise.

    > My point was the All C guaranteed was that every string is NULL
    > terminated.


    Be careful with your terms. Strings are null terminated, not NULL
    terminated. NULL is a macro which expands to a pointer --
    specifically a null pointer constant. Think of NULL as a noun (it is
    the name of a macro after all). The word "null" is an adjective --
    you can have a null pointer and a null character is used to terminate
    strings.

    --
    Ben.
    Ben Bacarisse, Jul 11, 2008
    #5
  6. Newbie

    Newbie Guest

    Ben Bacarisse wrote:
    > Newbie <> writes:
    > <snip>
    >> Thanks You all.The only thing i wanted to know was whether it was
    >> right to say "In C it is ASCII-Z representation and end with ASCII
    >> value 0"

    >
    > It may be daft to labour the point (you seem happy with the answers)
    > but the reason I did not answer this question is that I can't put any
    > meaning to the words. I can't link the two clauses with "and" and the
    > only meaning I can give to "ASCII-Z representation" (the number 90)
    > makes not sense in this context.
    >
    > One often reads things like "ASCII value 0". But the word "ASCII" has
    > no meaning here. ASCII value 0 is the value 0. It is a property of
    > all encodings that "Froodle value 90" is 90. What 90 *means* in the
    > Froodle character set is, of course, a matter for the Froodle
    > standards committee. If they decide that the character 'A' is to be
    > represented by the value 0, then it is not possible to use Froodle as
    > the execution character set for a C implementation because it is the
    > value 0 (regardless of any meaning it might have) that terminates
    > strings in C, and C insists that strings be both zero terminated and
    > be able to hold the character 'A'.
    >
    > Anyway, I am glad that you are content. If this is more than you
    > want to know, I apologise.
    >


    This was enlightening! Thank You.
    Newbie, Jul 11, 2008
    #6
  7. In article <>,
    Ben Bacarisse <> wrote:

    >only meaning I can give to "ASCII-Z representation" (the number 90)
    >makes not sense in this context.


    ASCII-Z is a term that means zero-terminated list of ASCII characters
    of indefinite size.

    It goes along with ASCII-C (I think it is) that means a list of ASCII
    characters prefixed with a size count; I'm not positive as to how big
    the count is, but 2 bytes is the figure that comes to mind.

    --
    "Whenever there is a hard job to be done I assign it to a lazy
    man; he is sure to find an easy way of doing it."
    -- Walter Chrysler
    Walter Roberson, Jul 11, 2008
    #7
  8. Ben Bacarisse <> writes:
    > In fact, an array can hold more than one string, but exactly how many
    > is a matter of definition. This array:
    >
    > char my_strings[] = { 'a', 'b', 'c', '\0', 'd', '\0' };
    >
    > has at least two strings in it: the C expressions my_strings and
    > my_strings + 4 both point to the start of valid strings


    Yes, since 6 is at least two. (I see that you addressed this below.)

    > (some people
    > choose to say that my_strings *is* a string -- feel free to do that in
    > casual conversation, but technically it is a pointer to the start of a
    > string).


    Yes and no. ``my_strings'' is the name of an object. That object is
    an array object, not a pointer object. On the other hand, if the name
    ``my_object'' is used as an expression, it will usually be implicitly
    converted to a pointer value (a char* pointing to the 'a'). On the
    other other hand, this conversion doesn't happen if ``my_object'' is
    the operand of a unary "&" or "sizeof" operator.

    So whether "my_object is a pointer" depends on whether you mean the
    object named "my_object" or the expression my_object.

    > my_strings + 1, my_strings + 2, and so on also point to (the start of)
    > strings, so that array can be said to hold 5 strings that would
    > compare as being different. It has two zero length strings
    > (my_strings + 3 and my_strings + 5) but they compare equal.


    --
    Keith Thompson (The_Other_Keith) <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"
    Keith Thompson, Jul 11, 2008
    #8
  9. -cnrc.gc.ca (Walter Roberson) writes:

    > In article <>,
    > Ben Bacarisse <> wrote:
    >
    >>only meaning I can give to "ASCII-Z representation" (the number 90)
    >>makes not sense in this context.

    >
    > ASCII-Z is a term that means zero-terminated list of ASCII characters
    > of indefinite size.


    Ah. Not an abbreviation I've come across. That makes it clear that
    the OP was only concerned to know that ASCII is not mandated by C.

    --
    Ben.
    Ben Bacarisse, Jul 12, 2008
    #9
  10. Keith Thompson <> writes:

    > Ben Bacarisse <> writes:
    >> In fact, an array can hold more than one string, but exactly how many
    >> is a matter of definition. This array:
    >>
    >> char my_strings[] = { 'a', 'b', 'c', '\0', 'd', '\0' };
    >>
    >> has at least two strings in it: the C expressions my_strings and
    >> my_strings + 4 both point to the start of valid strings

    >
    > Yes, since 6 is at least two. (I see that you addressed this
    > below.)


    I knew I'd have to!

    >> (some people
    >> choose to say that my_strings *is* a string -- feel free to do that in
    >> casual conversation, but technically it is a pointer to the start of a
    >> string).

    >
    > Yes and no. ``my_strings'' is the name of an object. That object is
    > an array object, not a pointer object. On the other hand, if the name
    > ``my_object'' is used as an expression, it will usually be implicitly
    > converted to a pointer value (a char* pointing to the 'a'). On the
    > other other hand, this conversion doesn't happen if ``my_object'' is
    > the operand of a unary "&" or "sizeof" operator.
    >
    > So whether "my_object is a pointer" depends on whether you mean the
    > object named "my_object" or the expression my_object.


    Yes. I did say "the expressions my_strings and my_strings + 4" but
    obviously I should have repeated "the expression" in the parenthetical
    remark to avoid ambiguity. I'll get the hang of c.l.c eventually.

    --
    Ben.
    Ben Bacarisse, Jul 12, 2008
    #10
  11. Ben Bacarisse <> writes:
    > -cnrc.gc.ca (Walter Roberson) writes:
    >> In article <>,
    >> Ben Bacarisse <> wrote:
    >>
    >>>only meaning I can give to "ASCII-Z representation" (the number 90)
    >>>makes not sense in this context.

    >>
    >> ASCII-Z is a term that means zero-terminated list of ASCII characters
    >> of indefinite size.

    >
    > Ah. Not an abbreviation I've come across. That makes it clear that
    > the OP was only concerned to know that ASCII is not mandated by C.


    The term I've seen is "asciz". It is, for example, an assembler
    directive; something like
    .asciz "hello"
    creates a constant containing "hello" followed by '\0', whereas
    .ascii "hello"
    might create a similar constant without the '\0'. (Not directly
    relevant to C, but relevant to understanding what's being discussed.)

    --
    Keith Thompson (The_Other_Keith) <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"
    Keith Thompson, Jul 12, 2008
    #11
    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. prem
    Replies:
    1
    Views:
    1,601
    Shankara Narayanan
    Nov 3, 2004
  2. HDSkiFreak
    Replies:
    2
    Views:
    3,530
    HDSkiFreak
    May 11, 2004
  3. Krishna
    Replies:
    5
    Views:
    1,782
    Howard
    Apr 1, 2005
  4. Tool69
    Replies:
    2
    Views:
    292
    Tool69
    Dec 15, 2006
  5. wizwx
    Replies:
    9
    Views:
    310
    David T. Ashley
    Feb 13, 2007
Loading...

Share This Page