473,320 Members | 1,881 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.

Latest Usenet C Argument

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
Dec 21 '07 #1
39 1516
Richard Heathfield wrote:
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.

Dec 21 '07 #2
On Dec 21, 11:35 am, Richard Heathfield <r...@see.sig.invalidwrote:
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.
Dec 21 '07 #3
vi******@gmail.com wrote:
On Dec 21, 11:35 am, Richard Heathfield <r...@see.sig.invalidwrote:
>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.
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[s]
himself.

Dec 21 '07 #4
On Dec 21, 2:35 pm, Richard Heathfield <r...@see.sig.invalidwrote:
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.
Dec 21 '07 #5
Richard Heathfield wrote:
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.

--
Ian Collins.
Dec 21 '07 #6
santosh said:
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[s]
himself.
Thanks, santosh. The question is already archived, btw - at
http://www.cpax.org.uk/prg/portable/c/luca/luca0.php

but Flash's Wiki might be a better home?

--
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
Dec 21 '07 #7
On Dec 21, 1:35 am, Richard Heathfield <r...@see.sig.invalidwrote:
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.)

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
Dec 21 '07 #8
In article <D5******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>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
--
:wq
Dec 21 '07 #9
Richard Heathfield wrote:
>
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.

--
pete
Dec 21 '07 #10
On 21 Dec, 09:35, Richard Heathfield <r...@see.sig.invalidwrote:
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...
Dec 21 '07 #11
I'm replying before reading any response, to prevent my answer being
prejudiced by them.

Richard Heathfield wrote:
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[i] 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[i][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[i][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.
Dec 21 '07 #12
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.)

--
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
Dec 21 '07 #13
gw****@aol.com wrote:
On 21 Dec, 09:35, Richard Heathfield <r...@see.sig.invalidwrote:
>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.
....
> D. None of the above, because...

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.
Dec 21 '07 #14
Richard Heathfield wrote:
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.)
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".
Dec 21 '07 #15
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.

--
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
Dec 21 '07 #16
Richard Heathfield wrote:
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).
But that's not what it is. It's not not-quite-right, for reasons which
I've already explained.
>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.
*bangs rocks together*
Dec 21 '07 #17
On Fri, 21 Dec 2007 09:35:36 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
>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.


Dec 21 '07 #18
Paul Hsieh wrote:
On Dec 21, 1:35 am, Richard Heathfield <r...@see.sig.invalidwrote:
>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. ;-)

--
Er*********@sun.com
Dec 21 '07 #19
On Dec 21, 8:30 pm, c...@tiac.net (Richard Harter) wrote:
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/t...xx/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.
Dec 21 '07 #20
Richard Heathfield <rj*@see.sig.invalidwrote:
# 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.

#include <stdio.h>

#define array(lwb,upb,Element,name) \
Element name##_ARRAY[(upb)-(lwb)+1], \
*name = name##_ARRAY-(lwb)

int main(int N,char **P) {
array(1,5,int,twice); int i;
for (i=1; i<=5; i++) twice[i] = 2*i;
for (i=1; i<=5; i++) printf("i=%d twice=%d\n",i,twice[i]);
return 0;
}

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I think that's kinda of personal; I don't think I should answer that.
Dec 21 '07 #21
SM Ryan wrote:
Richard Heathfield <rj*@see.sig.invalidwrote:
# 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.

#include <stdio.h>

#define array(lwb,upb,Element,name) \
Element name##_ARRAY[(upb)-(lwb)+1], \
*name = name##_ARRAY-(lwb)

int main(int N,char **P) {
array(1,5,int,twice); int i;
for (i=1; i<=5; i++) twice[i] = 2*i;
for (i=1; i<=5; i++) printf("i=%d twice=%d\n",i,twice[i]);
return 0;
}
Undefined behaviour. The offending expression is twice_ARRAY-(1).

n1256 6.5.6p9 states "If both the pointer operand and the result point
to elements of the same array object, or one past the last element of
the array object, the evaluation shall not produce an overflow;
otherwise, the behavior is undefined."

Phil
Dec 21 '07 #22
Philip Potter <pg*@doc.ic.ac.ukwrote:

# n1256 6.5.6p9 states "If both the pointer operand and the result point

That's nice.

It's also cow doo-doo as anyone who has worked on compilers knows. Any compiler
that can't deal with this should flushed down the toilet as the refuse it is.

Any compiler that requires the code displacement portion of a relative address
to be nonnegative is broken by design. This has been known since the fifties
and the first assemblers.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
No pleasure, no rapture, no exquisite sin greater than central air.
Dec 21 '07 #23
On Fri, 21 Dec 2007 08:15:42 -0800 (PST), harsha
<ha********@gmail.comwrote:
>On Dec 21, 8:30 pm, c...@tiac.net (Richard Harter) wrote:
>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/t...xx/EWD831.html
Probably - it's been a decade or two since I've read the book. I
have the vague impression that that wasn't the only time he
discussed the subject.
>
However according to the book "Expert C Programming, Deep C Secrets"
zero based indexing was put just for the benefit of compiler writers.
Dec 21 '07 #24
SM Ryan wrote:
Philip Potter <pg*@doc.ic.ac.ukwrote:

# n1256 6.5.6p9 states "If both the pointer operand and the result point

That's nice.

It's also cow doo-doo as anyone who has worked on compilers knows. Any compiler
that can't deal with this should flushed down the toilet as the refuse it is.
Perhaps, but the Standard allows such a compiler, and strictly
conforming code will work on such a compiler.
Any compiler that requires the code displacement portion of a relative address
to be nonnegative is broken by design. This has been known since the fifties
and the first assemblers.
C does not place such a requirement. a[-1] is perfectly well designed
provided that a is a pointer to any element but the first in an array.
Dec 21 '07 #25
In article <D5******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>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...
It falls naturally out of the definition of array indexing in terms of
pointer arithmetic.
This is close enough to (B) that I'm not going to commit to whether I'm
giving an explanation of (B) or a completion of (D) without more
coffee.

This isn't quite a reason for why array indexing was defined that way,
though. I suspect that the root cause is "because B/BCPL/the PDP-11
did it that way and nobody thought it was worth changing since".
dave

Dec 21 '07 #26
On 2007-12-21, dj******@csclub.uwaterloo.ca.invalid <dj******@csclub.uwaterloo.ca.invalidwrote:
In article <D5******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>>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...

It falls naturally out of the definition of array indexing in
terms of pointer arithmetic. This is close enough to (B) that
I'm not going to commit to whether I'm giving an explanation of
(B) or a completion of (D) without more coffee.
No, B is actually nonsense. Yes, there are no elements between
the first and the first element. But there are *also* no elements
between the first and the second element.
This isn't quite a reason for why array indexing was defined
that way, though. I suspect that the root cause is "because
B/BCPL/the PDP-11 did it that way and nobody thought it was
worth changing since".
I think the fact that array indexing is syntactic sugar for
pointer arithmetic neatly explains the reasoning. Imagine using
pointer arithmetic if a + 1 equaled a.

--
Neil Cerutti
Dec 21 '07 #27
In article <sl********************@FIAD06.norwich.edu>,
Neil Cerutti <ho*****@yahoo.comwrote:
>On 2007-12-21, dj******@csclub.uwaterloo.ca.invalid
<dj******@csclub.uwaterloo.ca.invalidwrote:
>In article <D5******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>>>Why does C use zero-based indexing?
[...]
>> 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.
>It falls naturally out of the definition of array indexing in
terms of pointer arithmetic. This is close enough to (B) that
I'm not going to commit to whether I'm giving an explanation of
(B) or a completion of (D) without more coffee.

No, B is actually nonsense. Yes, there are no elements between
the first and the first element. But there are *also* no elements
between the first and the second element.
Ahh. Yes, I was reading "between the first element in the array
and..." as "between the beginning of the array and...".

So it looks like I was at least right about needing more coffee.
dave

Dec 21 '07 #28
cr*@tiac.net (Richard Harter) writes:
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.
Algol had VLA with index starting where you wanted. Far more sophisticated
than C

See http://www.fh-jena.de/~kleine/histor...gol60-Naur.pdf

--
Jean-Marc
Dec 21 '07 #29
santosh wrote:
>
vi******@gmail.com wrote:
On Dec 21, 11:35 am, Richard Heathfield <r...@see.sig.invalidwrote:
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.

Would they? What if the compilers did the necessary translation. A
recompile would be all that's needed.
Are you sure?

What happens with for-loops starting at zero, and using the loop
variable as a subscript?

If "p[1]" is now the first element, what does "p[0]" mean?

What happens when "*p" and "p[1]" now mean the same thing?

What happens when "*(p+1)" and "p[1]" now mean different things?
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[s]
himself.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Dec 21 '07 #30
James Kuyper wrote:
>
gw****@aol.com wrote:
On 21 Dec, 09:35, Richard Heathfield <r...@see.sig.invalidwrote:
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.
...
D. None of the above, because...
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.
Well, let's all go back to day zero...

In the beginning Brian created the stdio.h and the main().

And the main() was without form, and void; and darkness was
upon the face of the kernel. And the Spirit of Brian moved upon
the face of the kernel.

And Brian said, Let there be int and there was int main().

And Brian saw the int, that it was good: and Brian divided the
int from the void.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Dec 21 '07 #31
Richard Heathfield wrote, On 21/12/07 10:05:
santosh said:
>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[s]
himself.

Thanks, santosh. The question is already archived, btw - at
http://www.cpax.org.uk/prg/portable/c/luca/luca0.php

but Flash's Wiki might be a better home?
I don't claim to own the Wiki, I only provide the server it runs on.
Having said that, yes, I think the Wiki would be a good home. Then
others who want to post more questions can do so.
--
Flash Gordon
Dec 21 '07 #32
Philip Potter wrote:
a[-1] is perfectly well designed
provided that a is a pointer to any element but the first in an array.
a[-1] is a valid lvalue if (a) points one past the array, also.

--
pete
Dec 21 '07 #33
On Dec 21, 9:35*am, Richard Heathfield <r...@see.sig.invalidwrote:
Why does C use zero-based indexing?
You claim that many people find zero-based indexing counter-intuitive.
Many people find any kind of indexing non-intuitive. However, look
back a few years. How many people were convinced that this millennium
started on Jan. 1st 2000 instead of Jan. 1st 2001? They just couldn't
understand that years form a one-based array.

Now seriously. If we wanted to have non-zero based arrays, they could
easily be added to C in a backwards compatible way. I won't try to
write down the syntax, but you could have

int a [1 : 10];

mean an array with ten elements, indexed from 1 to 10. And

int b [1900 : 2100, -5 : 5];

could mean a two-dimensional array of 201 x 11 elements, with the
first index ranging from 1900 to 2100, the second index from -5 to 5.
Such an array could only be used with the array [] operator, and for
multi-dimensional arrays (or all arrays with lower and upper bound),
comma expressions would not be allowed for indices, same as for
function parameters.

It wouldn't be possible to (reasonably) have an assignment

int *p = a;

Either p would point to the first array element, in which case p[0]
and a[1] would refer to the same array element, which would be more
than weird. On the other hand, p cannot be a pointer to int with the
property that p[1] and a[1] point to the same array element, because
you cannot have a pointer p such that p+1 points to the first element
of an array.

You could obviously write

int *p = &a [1];

which would work as everyone thinks. So with the "enhanced" array
syntax, you wouldn't get automatic decay from array to pointer, and
you wouldn't get the automatic decay from parameters looking like
arrays to parameters of pointer type.
Dec 22 '07 #34

"santosh" <sa*********@gmail.comwrote in message
news:fk**********@registered.motzarella.org...
Richard Heathfield wrote:
>Why does C use zero-based indexing?
<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.
This is a fallacy. Sometimes, an offset may incur a runtime penalty, but so
what? And it would be 'offset' by not needing a -1 in user code sometimes.

Having a choice of 0- or 1-based wouldn't have been difficult and would have
kept everyone happy except those wanting any base.
>
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.
Processors also have means for applying offsets to addresses.
>
I think answer C is the one I would choose, if I had to do so.
I would have said D (or F on the link), because 1-based (or a mix) would
have upset the special relationship between arrays and pointer arithmetic.

Bart

Dec 22 '07 #35
In article <47****************@news.sbtc.netcr*@tiac.net (Richard Harter) writes:
....
I have the impression
that Algol used 0 based and that BCPL was out of the Algol
tradition.
In Algol 60 you could use any base. Actually, in the array declaration
you had to supply both lower- and upper-bound of the index. Algol 68
was similar, but when the bound pair started with "1:", the "1:" could be
omitted. I do not think Algol 58 (from which BCPL derives) was sufficiently
developed to have any idea about it.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Dec 22 '07 #36
On Dec 21, 1:32*pm, Philip Potter <p...@doc.ic.ac.ukwrote:
Richard Heathfield wrote:
Why does C use zero-based indexing?
* *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[i][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[i][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.
Or in cases where one has to transform from 2-dimensional
to 1-dimensional arrays and vice versa. Once I was helping
a statistician friend who's rather weak at programming to
write some programmes in Matlab, a language I don't actually
know. As far as I could tell Matlab has fully fledged
multi-dimensional arrays , you don't have to fake anything,
but arrays are 1 based. As part of his statistical
manipulations he often had to transform from 2-d to 1-d or
vice versa and these 1-off corrections were driving me crazy.

Paul Hsieh also gave a good example elsethread.
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).
What makes you think that mathematicians prefer 1-based ?
I'm one and I prefer 0-based.
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.
Arrays in awk start from 1 ? That's news to me , the versions
of awk I'm familiar with use associative arrays so a "start"
cannot be defined in a natural way.
Any discussion on where array indexing should start ought
to mention the well known quote by Stan Kelly-Bootle:

"Should array indices start at 0 or 1? My compromise of
0.5 was rejected without, I thought, proper consideration."
Dec 22 '07 #37
Philip Potter <pg*@doc.ic.ac.ukwrote:
# SM Ryan wrote:
# Philip Potter <pg*@doc.ic.ac.ukwrote:
# >
# # n1256 6.5.6p9 states "If both the pointer operand and the result point
# >
# That's nice.
# >
# It's also cow doo-doo as anyone who has worked on compilers knows. Any compiler
# that can't deal with this should flushed down the toilet as the refuse it is.
#
# Perhaps, but the Standard allows such a compiler, and strictly
# conforming code will work on such a compiler.
#
# Any compiler that requires the code displacement portion of a relative address
# to be nonnegative is broken by design. This has been known since the fifties
# and the first assemblers.
#
# C does not place such a requirement. a[-1] is perfectly well designed

a[-1] and a-1 are different expressions. Any competent compiler optimiser
will analyze subscript into a constant and variant part, A[K+V], and allow
the precomputation of A+K on machines where this is a win so the run time
operation is only biasedA[V] adding the constant during compilation or load
instead of runtime. This is elementary compiler theory.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
A bunch of savages in this town.
Dec 22 '07 #38
SM Ryan wrote:
>
.... snip ...
>
a[-1] and a-1 are different expressions. Any competent compiler
optimiser will analyze subscript into a constant and variant part,
A[K+V], and allow the precomputation of A+K on machines where this
is a win so the run time operation is only biasedA[V] adding the
constant during compilation or load instead of runtime. This is
elementary compiler theory.
Actually, this is WRONG. The C standard specifies that array
accesses are to be converted into pointer + offset accesses. This
leaves only one variety to deal with.

Please fix your non-standard quote character. It makes it
impossible to have proper quotation through multiple levels.

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee.
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Dec 23 '07 #39
On Sat, 22 Dec 2007 04:38:06 -0800 (PST), Spiros Bousbouras
<sp****@gmail.comwrote:
On Dec 21, 1:32*pm, Philip Potter <p...@doc.ic.ac.ukwrote:
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.

Arrays in awk start from 1 ? That's news to me , the versions
of awk I'm familiar with use associative arrays so a "start"
cannot be defined in a natural way.
A few language-defined ones are integers:
ARGV [1..ARGC]
the result of split()
(in gawk) the result of asort() and asorti()
$1..NF if you count that as an array (it is in perl FWTW)

and (as a result?) IME it is usual (but not required) for user-defined
arrays whose indices _are_ an integer range to be 1-based.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Dec 30 '07 #40

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

Similar topics

4
by: Gleep | last post by:
Hey Guys, I've got a table called Outcomes. With 3 columns and 15 rows 1st col 2nd col 3rdcol outcome date price There are 15 rows...
5
by: Jagdip Singh Ajimal | last post by:
I have 6 columns, all with dates within them, i.e. Proposed Start Date 1 Proposed Start Date 2 Proposed Start Date 3 Proposed Finish Date 1 Proposed Finish Date 2 Proposed Finish Date 3 ...
8
by: John Blair | last post by:
Hi, I updated the latest .Net patch on my system this moring and now my SQL Web Data Administrator application is not working. I get the following error: Server Application Unavailable The...
7
by: Scott Frankel | last post by:
Still too new to SQL to have run across this yet ... How does one return the latest row from a table, given multiple entries of varying data? i.e.: given a table that looks like this: color...
76
by: kwilder | last post by:
This works, but it doesn't load the latest version of the xml if it was just modified without closing and reopening the browser. Here's the scenario: I have an xml doc called results.xml. It...
1
by: David W. Fenton | last post by:
I just read the latest post on the Access 12 Blog: Tracking Application Overview http://blogs.msdn.com/access/archive/2006/02/06/525888.aspx and just have to say: <Jon Stewart> Whuh?????...
42
by: Noah Roberts | last post by:
The latest MS compiler "depricates" certain functions including stricmp. The function stricmp is not part of the C++ standard, it is part of POSIX. The question I have, is it really necissary for...
224
by: Jon Slaughter | last post by:
Sorry for all the cross posting but I'm interesting in getting a serious discussion about how usenet has become lately. Many people are moving away from usenet because of all the spam and cooks...
19
by: jim | last post by:
(from http://www.news.com/8301-13578_3-9798715-38.html ) October 16, 2007 5:56 PM PDT RIAA tries to pull plug on Usenet. Seriously. Posted by Declan McCullagh The Recording Industry Association...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.