Newby question about arrays

Discussion in 'C Programming' started by CeyloR, Sep 3, 2005.

  1. CeyloR

    CeyloR Guest

    Hello everyone,

    I just started with the language C and it's very interesting.

    I bought the book 'The C Programming Language' from Brian W. Kernighan
    and Dennis M. Ritchie.

    While reading something about arrays in C, there was something not
    clear to me.

    In an example they use an array to count digits from some input. When
    they refer to the array the code looks like:

    ++ndigit[c-'0'];

    where c is the current character beeing read from the input (as
    integer).

    Am I right that the [c-'0'] stands for the convertion of the character
    to it's integer value?

    Any help is welcome.
     
    CeyloR, Sep 3, 2005
    #1
    1. Advertising

  2. CeyloR

    gooch Guest

    > ++ndigit[c-'0'];
    >
    > where c is the current character beeing read from the input (as
    > integer).
    >
    > Am I right that the [c-'0'] stands for the convertion of the character
    > to it's integer value?


    c - '0' gives the index into the array. When a character is entered by the
    user, say '5', the real value of this stored in the computer is not the
    integer value 5. For ASCII characters, I believe, '0' = 32 '1' = 33 and so
    on. So the values are all consecutive and '5' - '0' really means 37 - 32 = 5
    and that is used to index into the 6th element of the array ndigit[5]. The
    value contained there is then incremented by the ++ operator.
     
    gooch, Sep 3, 2005
    #2
    1. Advertising

  3. In article <>,
    CeyloR <> wrote:

    >++ndigit[c-'0'];


    >where c is the current character beeing read from the input (as
    >integer).


    >Am I right that the [c-'0'] stands for the convertion of the character
    >to it's integer value?


    Not -exactly- but close.

    c-'0' is certain to convert the character representation of
    a digit into the numeric value represented... '0' to 0, '9' to 9
    and so on.

    The C language standards say almost nothing about internal
    representation of characters: one of the very few things they
    do promise is that the representation of the 10 digits will be
    sequential and ascending starting from 0. The language standards
    don't specify whether that sequence starts from 48 or from
    176 or from 432493, just that they are in sequence.

    The language standards do NOT make this promise for any other
    characters -- the value of 'E' could be 19 less than the value of
    'D' and the '$' character does not have to exist at all, for
    example: only the digits are pinned down at all.


    Anyhow, the [] don't have anything to do with the numeric
    conversion process. The [] is the syntax for array indexing. c-'0'
    produces an integral value, and ndigit[thevalue] indexes to
    that location relative to the beginning of the ndigit array,
    and then the ++ operator increments the value stored at that
    location.
    --
    I was very young in those days, but I was also rather dim.
    -- Christopher Priest
     
    Walter Roberson, Sep 3, 2005
    #3
  4. In article <>,
    gooch <> wrote:
    :For ASCII characters, I believe, '0' = 32 '1' = 33 and so
    :eek:n.

    No, in ASCII, '0' is 48. 32 is space in ASCII, then
    !"#$%&'()*+,-./ all before '0'.
    --
    Look out, there are llamas!
     
    Walter Roberson, Sep 3, 2005
    #4
  5. CeyloR

    gooch Guest

    "> :For ASCII characters, I believe, '0' = 32 '1' = 33 and so
    > :eek:n.
    >
    > No, in ASCII, '0' is 48. 32 is space in ASCII, then
    > !"#$%&'()*+,-./ all before '0'.


    Sorry about that. But for the OP, the concept is still the same, just change
    the values.
     
    gooch, Sep 3, 2005
    #5
  6. CeyloR

    CeyloR Guest

    Ok that makes sense.

    so in this case the c -'0' is only done to give array ndigit an index
    between 0 and 9.

    For example if c = 1 in ASCII it's real value is 49, the 0 in ASCII
    stands for integer 48.

    c-'0' then means 49-48, gives array ndigit an indez of 1.

    I think I've seen the light :)
     
    CeyloR, Sep 3, 2005
    #6
  7. CeyloR

    Guest

    The read in values are ascii text, they are not numeric. by subtracting
    the value of '0' you get the integer value. (also remember characters
    underneath are just numbers)
     
    , Sep 3, 2005
    #7
  8. CeyloR wrote on 03/09/05 :
    > Am I right that the [c-'0'] stands for the convertion of the character
    > to it's integer value?


    If you meant "conversion", "its" and "integral" , yes!

    It works because in C, digit character values must be consecutive.
    Doesn't work for letters.


    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    ..sig under repair
     
    Emmanuel Delahaye, Sep 3, 2005
    #8
  9. gooch wrote on 03/09/05 :
    >> ++ndigit[c-'0'];

    > c - '0' gives the index into the array.


    No. c - '0' gives the integral value of c if its value represents a
    digit ('0'-'9').

    [c - '0'] gives an index of some array...

    > When a character is entered by the
    > user, say '5', the real value of this stored in the computer is not the
    > integer value 5. For ASCII characters, I believe, '0' = 32 '1' = 33 and so
    > on. So the values are all consecutive and '5' - '0' really means 37 - 32 = 5
    > and that is used to index into the 6th element of the array ndigit[5]. The
    > value contained there is then incremented by the ++ operator.



    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "It's specified. But anyone who writes code like that should be
    transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
     
    Emmanuel Delahaye, Sep 3, 2005
    #9
  10. On 3 Sep 2005 08:58:04 -0700, "CeyloR" <> wrote:

    >Ok that makes sense.
    >
    >so in this case the c -'0' is only done to give array ndigit an index
    >between 0 and 9.
    >
    >For example if c = 1 in ASCII it's real value is 49, the 0 in ASCII
    >stands for integer 48.


    NO! If c =1, then its real value is 1. If c = '1', then its real
    value in ASCII would be 49 and in EBCDIC 241. There could be other
    character encoding schemes.

    >
    >c-'0' then means 49-48, gives array ndigit an indez of 1.


    Only when c is '1' and you are on an ASCII system.

    >
    >I think I've seen the light :)


    But you must remove your sunglasses.


    <<Remove the del for email>>
     
    Barry Schwarz, Sep 3, 2005
    #10
    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. Hermann W Ehlers

    Newby question

    Hermann W Ehlers, May 2, 2004, in forum: ASP .Net
    Replies:
    12
    Views:
    711
    Matt Berther
    May 5, 2004
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    ASP.NET easy newby question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Nov 22, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    481
    Kevin Spencer
    Nov 23, 2004
  3. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    658
    Iain Norman
    Feb 16, 2005
  4. Blue Streak
    Replies:
    3
    Views:
    531
    S. Justin Gengo
    Dec 12, 2005
  5. Philipp
    Replies:
    21
    Views:
    1,133
    Philipp
    Jan 20, 2009
Loading...

Share This Page