473,603 Members | 2,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4042
On 16 Jul 2003 18:52:18 -0700, gu*********@ver izon.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
2157
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 operator for cFieldList as so: cField& operator(int pos) { return myvec; }
5
1882
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) {tListOf.push_back(t);} // add new object to list unsigned int Count() { return tListOf.size(); } // number of list items
4
10449
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 an unsigned integer, so I see a +1 if the bit is set. Microsoft VC++ thinks it's signed, so I see -1 if the bit is set. Ex. typedef enum {
6
13813
by: sarathy | last post by:
Hi, What is integer promotion? How is it different from arithmetic conversion? Regards, Sarathy
34
11289
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. Could you know any other more efficient way? Cuthbert
232
13218
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 set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
6
2724
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 based on the const type. Infact if I use the Array on the left side i.e like a1 = 111 then it uses the first while if I use cout << a1 it uses the second... why????
3
14556
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' for array subscript hexdmp.cpp:146: error: invalid types `unsigned char' for array subscript hexdmp.cpp:146: error: invalid types `unsigned char' for array
17
1850
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: ============================ #include <stdio.h> void fcn (char *str)
17
5238
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, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3); aa = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3); augin = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3+MAXSIMS); augout = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3+MAXSIMS);
0
7928
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8415
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8273
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6735
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5441
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3903
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.