473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Newby question about arrays

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.

Nov 15 '05 #1
9 1345
> ++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.
Nov 15 '05 #2
In article <11**********************@g14g2000cwa.googlegroups .com>,
CeyloR <w.*****@gmail.com> 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
Nov 15 '05 #3
In article <St********************@comcast.com>,
gooch <go******@comcast.net> wrote:
:For ASCII characters, I believe, '0' = 32 '1' = 33 and so
:on.

No, in ASCII, '0' is 48. 32 is space in ASCII, then
!"#$%&'()*+,-./ all before '0'.
--
Look out, there are llamas!
Nov 15 '05 #4

"> :For ASCII characters, I believe, '0' = 32 '1' = 33 and so
:on.

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.
Nov 15 '05 #5
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 :-)

Nov 15 '05 #6
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)

Nov 15 '05 #7
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
Nov 15 '05 #8
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
Nov 15 '05 #9
On 3 Sep 2005 08:58:04 -0700, "CeyloR" <w.*****@gmail.com> 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>>
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Damien | last post by:
I have just built a simple stopwatch application, but when i f5 to get things goings i get this message, An unhandled exception of type 'System.ArithmeticException' occurred in...
0
by: marco | last post by:
I'm trying to parse a xml bookmarkpage with php. I found a very useful example script about how you can parse a xml document with php. The scriptworks really smooth. The xml test document (See...
8
by: Ask | last post by:
G'day All, Just thought I'd drop in and say hi. I'm new to Python, but old to software development. Python is one of the languages used in my new job, so I've just bought a book, read it, and...
7
by: Fred Nelson | last post by:
Hi: I'm more than a little confused with this question: I need to create a char type from the first letter in a string - for example: string mystring; char mychar;
10
by: Fred Nelson | last post by:
Hi: I have programmed in VB.NET for about a year and I'm in the process of learing C#. I'm really stuck on this question - and I know it's a "newby" question: In VB.NET I have several...
4
by: Boni | last post by:
Hi, How do I create an array of 2 to object pointers? i.e. dim a as object(2) Thanks, Boni
12
by: Boni | last post by:
Dear all, How can I add something to the VB arrays after the initialization. dim _points() as System.Drawing.points If it is not possible how can I pass into function, with VB array as an...
2
by: Fred Nelson | last post by:
Hi: I'm working on a VS2005 web application and I have what is probabably a "newby" question. In VS2003 I could drag a textbox/button/etc on to a form and position it with the mouse. I...
5
by: alexrixhardson | last post by:
Hi guys, I am a newby in the C/C++ world, and I am beginning to work on a rather simple TCP/IP proxy application which must be able to handle large volume of data as quickly as possible. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.