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

1. ### CeyloRGuest

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.

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

2. ### goochGuest

> ++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

3. ### Walter RobersonGuest

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
4. ### Walter RobersonGuest

In article <>,
gooch <> wrote:
:For ASCII characters, I believe, '0' = 32 '1' = 33 and so
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
5. ### goochGuest

"> :For ASCII characters, I believe, '0' = 32 '1' = 33 and so
> 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
6. ### CeyloRGuest

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
7. ### 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
8. ### Emmanuel DelahayeGuest

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
9. ### Emmanuel DelahayeGuest

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
10. ### Barry SchwarzGuest

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