Latest Usenet C Argument

R

Richard Heathfield

Why does C use zero-based indexing?

Many people find zero-based indexing difficult and non-intuitive. Why
doesn't C start indices from 1 instead? This would be much easier for
people.

A. It doesn't. At least, it doesn't have to. See ISO/IEC 9899:1999
6.5.2.1 'Array subscripting', which doesn't even mention zero-based
indexing. The indexing base is implementation-defined, and is often
configurable via a compiler switch, a bit like OPTION BASE 0 / OPTION BASE
1 in various BASICs.
B. An array index is merely another way of describing the number of
objects that sit between the first element in the array and the element we
wish to access. Clearly, if we are trying to access the first element, the
number of objects between is 0, so it makes sense to use 0 as the array
index for the first element.
C. One-based indexing leads to unnecessarily complicated arithmetic,
especially when dealing with multi-dimensional arrays. These complications
just fall away into nothing if zero-based indexing is used.
D. None of the above, because...
 
S

santosh

Richard said:
Why does C use zero-based indexing?

Many people find zero-based indexing difficult and non-intuitive. Why
doesn't C start indices from 1 instead? This would be much easier for
people.

<snip>

Because array access in C is done in terms of pointer arithmetic. If
indexing began with 1, compilers would very likely need to do an
unnecessary subtraction step in implementing the array access.

C aims to "deal with the same sort of abstractions" as real machines,
and most hardware architectures start memory addressing at zero. C
follows this for the sake of efficiency and logical clarity.

I think answer C is the one I would choose, if I had to do so.
 
V

vippstar

Why does C use zero-based indexing?

Many people find zero-based indexing difficult and non-intuitive. Why
doesn't C start indices from 1 instead? This would be much easier for
people.
People that find the concept of "zero-based indexing" (which doesn't
even exist in C as you mentioned, it's pointer arith) should simply
avoid C.
Plus if that changes now, many C programs would break.
So.. no matter how much you complain and how many points you make, it
won't happen, not in C.
 
S

santosh

People that find the concept of "zero-based indexing" (which doesn't
even exist in C as you mentioned, it's pointer arith) should simply
avoid C.
Plus if that changes now, many C programs would break.

Would they? What if the compilers did the necessary translation. A
recompile would be all that's needed.
So.. no matter how much you complain and how many points you make, it
won't happen, not in C.

Perhaps you missed the wood for the trees? Richard started this thread
in respose to the other thread asking for regular "quiz" questions.
This is a quiz question. He is very well aware of the answer
himself.
 
H

harsha

Why does C use zero-based indexing?

Many people find zero-based indexing difficult and non-intuitive. Why
doesn't C start indices from 1 instead? This would be much easier for
people.

A. It doesn't. At least, it doesn't have to. See ISO/IEC 9899:1999
6.5.2.1 'Array subscripting', which doesn't even mention zero-based
indexing. The indexing base is implementation-defined, and is often
configurable via a compiler switch, a bit like OPTION BASE 0 / OPTION BASE
1 in various BASICs.
B. An array index is merely another way of describing the number of
objects that sit between the first element in the array and the element we
wish to access. Clearly, if we are trying to access the first element, the
number of objects between is 0, so it makes sense to use 0 as the array
index for the first element.
C. One-based indexing leads to unnecessarily complicated arithmetic,
especially when dealing with multi-dimensional arrays. These complications
just fall away into nothing if zero-based indexing is used.
D. None of the above, because...

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

I think it is option D.
C uses zero based indexing because it is easier for compiler writers
who like to think in terms of offsets.The starting element of an array
will have an offset of zero and so it is indexed zero.
 
I

Ian Collins

Richard said:
Why does C use zero-based indexing?

Many people find zero-based indexing difficult and non-intuitive. Why
doesn't C start indices from 1 instead? This would be much easier for
people.

A. It doesn't. At least, it doesn't have to. See ISO/IEC 9899:1999
6.5.2.1 'Array subscripting', which doesn't even mention zero-based
indexing. The indexing base is implementation-defined, and is often
configurable via a compiler switch, a bit like OPTION BASE 0 / OPTION BASE
1 in various BASICs.
B. An array index is merely another way of describing the number of
objects that sit between the first element in the array and the element we
wish to access. Clearly, if we are trying to access the first element, the
number of objects between is 0, so it makes sense to use 0 as the array
index for the first element.
C. One-based indexing leads to unnecessarily complicated arithmetic,
especially when dealing with multi-dimensional arrays. These complications
just fall away into nothing if zero-based indexing is used.
D. None of the above, because...
D, the real reason is K&R starts from chapter 0.
 
P

Paul Hsieh

Why does C use zero-based indexing?

Many people find zero-based indexing difficult and non-intuitive. Why
doesn't C start indices from 1 instead? This would be much easier for
people.

No. Besides having an obvious tiling property, consider the following
problem:

Suppose you arrange a sorted list of numbers onto pages, 10 per page
in a book. Now you want to take those same numbers and put them into
another list with 13 per page. If an element was at position i on
page p of the first book, what page does it appear on in the second
book?

With 0-indexing the problem is just a bunch of simplistic modulo
arithmetic. With 1-indexing, you have extra +1's and -1's and you
might have to special case the one of the end of each page (or at
least you have examine all the edge cases a lot more carefully to get
the mapping right.)

Over the years, I have run into this and similar problems for which I
am grateful for C's typical idiom of being 0-indexed rather than 1-
indexed.

I have recently started embracing the language Lua in a very serious
way. Unfortunately, they have chosen 1-based array indexing. Or well,
not exactly, but many of their range idioms (for loops, etc.) are 1-
based instead of 0-based. I find that its *mostly* not a problem,
until you have to solve a problem such as the one described above.
The way I solve it, is that I write pseudo-code for a 0-based array
solution, then translate it to 1-based, then simplify the arithmetic
and implement it -- I end up having slight "off by 1" adjustments
which are hard to explain in a just few comments. (Its the only
really serious weakness I find in that language.)
 
R

Richard Tobin

Richard Heathfield said:
Why does C use zero-based indexing?

Are you looking for a historical, teleological, or psychoanalytic
explanation?

I'll go with Bismarck's comment on the Franco-Prussian war: it was in
the logic of history.

-- Richard
 
P

pete

Richard said:
Why does C use zero-based indexing?

Many people find zero-based indexing difficult and non-intuitive. Why
doesn't C start indices from 1 instead? This would be much easier for
people.

A. It doesn't. At least, it doesn't have to. See ISO/IEC 9899:1999
6.5.2.1 'Array subscripting', which doesn't even mention zero-based
indexing. The indexing base is implementation-defined, and is often
configurable via a compiler switch,
a bit like OPTION BASE 0 / OPTION BASE
1 in various BASICs.
B. An array index is merely another way of describing the number of
objects that sit between the first element in the array and the
element we
wish to access. Clearly,
if we are trying to access the first element, the
number of objects between is 0, so it makes sense
to use 0 as the array
index for the first element.
C. One-based indexing leads to unnecessarily complicated
arithmetic,
especially when dealing with multi-dimensional arrays. These > complications
just fall away into nothing if zero-based indexing is used.
D. None of the above, because...

E. All of the above(including choice D.)

Concerning the original premise:
"Many people find zero-based indexing difficult and non-intuitive."

Zero-based indexing is non-intuitive.
First Prize has a number one on it.
Ordinarilly we expect that the third and fourth
will be numbered as three and four.

As for zero-based indexing being difficult,
I think that's more of a phobia than a real problem,
as is suggested by choice C.
 
G

gw7rib

Why does C use zero-based indexing?

Many people find zero-based indexing difficult and non-intuitive. Why
doesn't C start indices from 1 instead? This would be much easier for
people.

   A. It doesn't. At least, it doesn't have to. See ISO/IEC 9899:1999
6.5.2.1 'Array subscripting', which doesn't even mention zero-based
indexing. The indexing base is implementation-defined, and is often
configurable via a compiler switch, a bit like OPTION BASE 0 / OPTION BASE
1 in various BASICs.
   B. An array index is merely another way of describing the number of
objects that sit between the first element in the array and the element we
wish to access. Clearly, if we are trying to access the first element, the
number of objects between is 0, so it makes sense to use 0 as the array
index for the first element.
   C. One-based indexing leads to unnecessarily complicated arithmetic,
especially when dealing with multi-dimensional arrays. These complications
just fall away into nothing if zero-based indexing is used.
   D. None of the above, because...

You've missed one possible answer...

C uses zero-based indexing because BCPL uses zero-based indexing and
because this wasn't changed when BCPL was changed into C.

Incidentally, BCPL's handling of arrays was a little different from
C's. The statement LET V = VEC 5 would actually reserve 6 consecutive
locations, so you could use indexes of 0-4 or 1-5 or even 0-5 if you
wanted to. For good measure, it also created a variable V as well,
which started off pointing at the 6 reserved locations but could be
made to point elsewhere if you wanted...
 
P

Philip Potter

I'm replying before reading any response, to prevent my answer being
prejudiced by them.

Richard said:
Why does C use zero-based indexing?

Many people find zero-based indexing difficult and non-intuitive. Why
doesn't C start indices from 1 instead? This would be much easier for
people.

A. It doesn't. At least, it doesn't have to. See ISO/IEC 9899:1999
6.5.2.1 'Array subscripting', which doesn't even mention zero-based
indexing. The indexing base is implementation-defined, and is often
configurable via a compiler switch, a bit like OPTION BASE 0 / OPTION BASE
1 in various BASICs.

This is nonsense, at least according to n1256 and n869. Both say that
x is equivalent to (*((x)+(i))), and when i == 0, x[0] == *x. If x is
an array, in this context x decays to a pointer to its first member, and
dereferencing that pointer yields the lvalue of the first member.

[Explanation of n-numbers: The official C Standards cost money, but you
can get draft C standards for free. n869 is the name of the last draft
before C99, and n1256 is the current latest draft, which is C99 plus
Technical Corrigenda TC1,TC2 and TC3. The drafts are useful for general
knowledge, but since the Standard is final and definitive,
implementations should be judged against that. ISO/IEC 9899:1999 above
refers to the C99 standard.]
B. An array index is merely another way of describing the number of
objects that sit between the first element in the array and the element we
wish to access. Clearly, if we are trying to access the first element, the
number of objects between is 0, so it makes sense to use 0 as the array
index for the first element.

Another way of saying this is that array subscripts are offsets rather
than indices - they are a measure of distance, not a count. However this
solution merely defines the problem away - if we give array indices a
definition which requires them to be zero-indexed, then of course they
should be zero-indexed!

This is not a justification for zero-based indexing, but it /is/ a
useful way of thinking of C indices.
C. One-based indexing leads to unnecessarily complicated arithmetic,
especially when dealing with multi-dimensional arrays. These complications
just fall away into nothing if zero-based indexing is used.

I can't imagine how. Here is a loop over a multidimensional array:
for(i = 0; i < isize; i++)
for(j = 0; j < jsize; j++)
for(k = 0; k < ksize; k++)
f(x[j][k]);

In a C where arrays are indexed from one, the same loop is as follows:
for(i = 1; i <= isize; i++)
for(j = 1; j <= jsize; j++)
for(k = 1; k <= ksize; k++)
f(x[j][k]);

Perhaps this answer is referring to multidimensional arrays which are
faked through single-dimension arrays, in a manner such as:

x[i*rowsize+j]

which accesses the cell at (i,j) where 0 <= i < rowsize and 0 <= j <
colsize, and x has size rowsize*colsize. The same index calculation for
one-based arrays is:

x[(i-1)*rowsize+j]

where 1 <= i <= rowsize and 1 <= j <= colsize.

The abstract machine has to implement different rules to implement the
one-based indexing. Here x[n] is equivalent to *(x+n-1), so every array
access adds one decrement operation to each array subscript. This is
intrinsic in the fact that arrays in C are almost equivalent to pointers
to the first element of an array.
D. None of the above, because...

If we accept the rule that C arrays decay to
pointers-to-the-first-element, then answer C. above shows why it is
logical for x[0] to be equivalent to *(x+0). Because C is a systems
language, and not only provides pointers but relies on them to
implmement arrays, it is natural that its arrays be zero-indexed.

Other languages are able to be one-indexed because their focus is
different; the arithmetic inefficiencies introduced by one-based
indexing either do not arise or are inconsequential compared to making
the language more familiar to those who prefer one-based indexing (such
as mathematicians).

Old versions of Perl allowed you to change the starting array index
through the $[ variable. It defaulted to 0, but allowed you to change it
to 1 to make it more familiar to awk and Fortran users. This is now
deprecated as a Bad Idea.
 
R

Richard Heathfield

Philip Potter said:
I'm replying before reading any response, to prevent my answer being
prejudiced by them.

I think I could give you 1.5 / 3, Philip. I don't think you read B closely
enough. (B is not quite as nonsensical as A, but it's still wrong.)
 
J

James Kuyper

You've missed one possible answer...

That's covered by D.
C uses zero-based indexing because BCPL uses zero-based indexing and
because this wasn't changed when BCPL was changed into C.

That merely moves the question back one step. Why did BCPL use
zero-based indexing? Keep recursing backwards as needed until you find
an actual reason.
 
P

Philip Potter

Richard said:
Philip Potter said:


I think I could give you 1.5 / 3, Philip. I don't think you read B closely
enough. (B is not quite as nonsensical as A, but it's still wrong.)

Your stock answer merely shows that the language is woolly, but I
consider my interpretation to be as valid as yours. "between" is a
frequently misused preposition, and so I tend to infer what the writer
meant rather than read what the writer literally said.

I don't think that B is "Not far off" as your stock answer says, because
even when the wording is corrected [1], it just defines the problem away
rather than justifying the design of the language. The problem is
reduced to "Why are indices defined as another way of describing the
number of elements preceding the element we wish to access?" but not
solved properly. Note that other languages are quite happy with other
definitions for indices, so this definition is not obviously correct. As
a result, I would not give you full marks either.

(How come noone else has been given marks?)

[1] To something like "An array index describes the number of elements
preceding the element we wish to access".
 
R

Richard Heathfield

Philip Potter said:
"between" is a
frequently misused preposition, and so I tend to infer what the writer
meant rather than read what the writer literally said.

But the writer was ME! So I don't need to infer anything; I know
*precisely* what the author meant, and I wrote precisely what I intended
to write (i.e. a not-quite-right answer).

Incidentally, regardless of the frequent errors of others, I am not
accustomed to misusing "between". I make no claim to perfection in all
matters, but I do know what "between" means.
I would not give you full marks either.

Neither would I, so we are in full agreement there.
(How come noone else has been given marks?)

Cos you and me, we da smart ones, dat why.
 
P

Philip Potter

Richard said:
Philip Potter said:


But the writer was ME! So I don't need to infer anything; I know
*precisely* what the author meant, and I wrote precisely what I intended
to write (i.e. a not-quite-right answer).

But that's not what it is. It's not not-quite-right, for reasons which
I've already explained.
Neither would I, so we are in full agreement there.
:)


Cos you and me, we da smart ones, dat why.

*bangs rocks together*
 
R

Richard Harter

Why does C use zero-based indexing?

Many people find zero-based indexing difficult and non-intuitive. Why
doesn't C start indices from 1 instead? This would be much easier for
people.

A. It doesn't. At least, it doesn't have to. See ISO/IEC 9899:1999
6.5.2.1 'Array subscripting', which doesn't even mention zero-based
indexing. The indexing base is implementation-defined, and is often
configurable via a compiler switch, a bit like OPTION BASE 0 / OPTION BASE
1 in various BASICs.
B. An array index is merely another way of describing the number of
objects that sit between the first element in the array and the element we
wish to access. Clearly, if we are trying to access the first element, the
number of objects between is 0, so it makes sense to use 0 as the array
index for the first element.
C. One-based indexing leads to unnecessarily complicated arithmetic,
especially when dealing with multi-dimensional arrays. These complications
just fall away into nothing if zero-based indexing is used.
D. None of the above, because...

D - Historical. Whether to use 0 based or 1 based is a language
design question. I can't find my copy of Dijkstra at the moment
but he had some marvelously snarky comments about why 0 based is
better. Fortran used 1 based, though I believe that the later
versions let you use anything you like. I have the impression
that Algol used 0 based and that BCPL was out of the Algol
tradition.

As to the arguments one way or another, in 0 based an index is an
offset, in 1 based it is an ordinal number. The reason that
ordinals are 1 based is because that way they map directly to
cardinal numbers, i.e., the nth item in a series is the last of n
items. This is more convenient and less confusing in ordinary
usage, though not, perhaps, in programming.
 
E

Eric Sosman

Paul said:
Why does C use zero-based indexing?

Many people find zero-based indexing difficult and non-intuitive. Why
doesn't C start indices from 1 instead? This would be much easier for
people.

No. [...]

Well, *some* people find it difficult. I've seen more
than one Heapsort implementation whose authors seemed to
regard one-based indexing as algorithmically indispensable.
(It isn't, of course, but ...)
I have recently started embracing the language Lua in a very serious
way.

I hope that she reciprocates your feeling and that
your intentions are honorable. ;-)
 
H

harsha

D - Historical. Whether to use 0 based or 1 based is a language
design question. I can't find my copy of Dijkstra at the moment
but he had some marvelously snarky comments about why 0 based is
better.

This might be what you are talking about:
link: http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

However according to the book "Expert C Programming, Deep C Secrets"
zero based indexing was put just for the benefit of compiler writers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top