473,320 Members | 1,976 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,320 software developers and data experts.

integer expression in a subscript

Hello all,

I'm having a bit of trouble understanding a snippet of code in the
K&R book I'm reading. Basically it's something like this:

int ndigit[10];
for (i = 0; i < 10; i++)
ndigit[i] = 0;

int c;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];

.... The problem is with the last line here. Why is it that the
substraction is needed here? Why couldn't it just be ++ndigit[c]? I
may be missing something totally here.

Thanks,
Gus
Nov 13 '05 #1
3 4019
On 16 Jul 2003 18:52:18 -0700, gu*********@verizon.net (Gus Tabares)
wrote:
Hello all,

I'm having a bit of trouble understanding a snippet of code in the
K&R book I'm reading. Basically it's something like this:

int ndigit[10];
for (i = 0; i < 10; i++)
ndigit[i] = 0;

int c;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];

... The problem is with the last line here. Why is it that the
substraction is needed here? Why couldn't it just be ++ndigit[c]? I
may be missing something totally here.

The subtraction has the affect of converting the value in c which is
the character representation of a digit (in ASCII, '0' is 0x30 or 48)
to the numeric value of the digit. This is guaranteed to work because
the standard requires the representation of the 10 digits to be
sequential in the normal order.

Thus, if you type a two, c will be set to '2' and the statement in
question will increment the value of ndigit[2] by 1. Without the
subtraction, it would attempt to increment the value of ndigit[50] (or
ndigit[242] if your machine is EBCDIC) which doesn't exist.
<<Remove the del for email>>
Nov 13 '05 #2
Gus Tabares wrote:

Hello all,

I'm having a bit of trouble understanding a snippet of code in the
K&R book I'm reading. Basically it's something like this:

int ndigit[10];
for (i = 0; i < 10; i++)
ndigit[i] = 0;

int c;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];

... The problem is with the last line here. Why is it that the
substraction is needed here? Why couldn't it just be ++ndigit[c]? I
may be missing something totally here.


(c) is equal to some number from '0' to '9', inclusive,
at that point in the code, which implies that
(c - '0') is equal to some number from 0 to 9, inclusive,
at that point in the code.

--
pete
Nov 13 '05 #3
Gus Tabares wrote:


I'm having a bit of trouble understanding a snippet of code in the
K&R book I'm reading. Basically it's something like this:

int ndigit[10];
for (i = 0; i < 10; i++)
ndigit[i] = 0;

int c;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];

... The problem is with the last line here. Why is it that the
subtraction is needed here? Why couldn't it just be ++ndigit[c]?
I may be missing something totally here. cat main.c #include<stdio.h>

int main(int argc, char* argv[]) {
int c = '0';
fprintf(stdout, "%d = c\n", c);
return 0;
}
gcc -Wall -std=c99 -pedantic -O2 -o main main.c
./main

48 = c

Nov 13 '05 #4

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

Similar topics

15
by: Steve | last post by:
Hi, I hope someone can help. I have a class called cField, and another class called cFieldList. cFieldList contains a std::vector of cFields called myvec I've overloaded the subscript...
5
by: Steve | last post by:
Hi, I have a class called cList as so: template<class T> class cList { // base class for Lists private: protected: vector<T> tListOf; // field list container public: void Add(const T& t)...
4
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is...
6
by: sarathy | last post by:
Hi, What is integer promotion? How is it different from arithmetic conversion? Regards, Sarathy
34
by: Cuthbert | last post by:
Hi folks, I am trying to find a more efficient way to count "How many bits are '1' in a integer variable?". I still have no idea to count the bits except using a loop and "if" statements....
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
6
by: josh | last post by:
Hi I've a dubt! when we have overloaded functions the compiler chooses the right being based on the argument lists...but when we have two subscript overloaded functions it resolves them being...
3
by: uche | last post by:
Please give me some feed back on this issue: Here is the complier error: hexdmp.cpp: In function `void output(unsigned char, int, bool&)': hexdmp.cpp:133: error: invalid types `unsigned char'...
17
by: matevzb | last post by:
I've ran into some fishy code that, at first glance, is buggy, but it seems to work correctly and none of the compilers I've tried (five so far, on various systems) gives any warnings. The code:...
17
by: Ivan K. | last post by:
I am looking at some legacy code, which begins by allocating a double matrix with the dmatrix() function from NRC as follows: double **A, **augin, **augout, **aa; A = dmatrix(1,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.