Velocity Reviews > Why index starts in C from 0 and not 1

# Why index starts in C from 0 and not 1

E. Robert Tisdale
Guest
Posts: n/a

 08-16-2004
Lew Pitcher wrote:

> Thomas Stegen wrote:
>
>>kapilk wrote:
>>
>>> I know that the array index starts in C from 0 and not 1.
>>> Can anybody please tell me the reason?
>>>
>>> Is it because in the subscript i can have a unsigned integer
>>> and these start from 0

No.

>> Maybe because the index value is really
>> the offset from the start of the array...

>
> Bingo!
>
> "Rather more surprising, at least at first sight,
> is the fact that a reference to a[i] can also be written as *(a+i).
> In evaluating a[i], C converts it to *(a+i) immediately;
> the two forms are completely equivalent.
> Applying the operator & to both parts of this equivalence,
> it follows that &a[i] and a+i are identical:
> a+i is the address of the i-th element beyond a."
> (from Section 5.3 of "The C Programming Language"
> by Brian W. Kernighan and Dennis M. Ritchie, (c) 197
>
> So, the genesis of C has a+i being the same as a[i].
> If a is an array, then &a[1] is the same as a+1,
> and thus a+0 must be the same as &a[0]. This makes arrays zero based.
>
> This is not to say that the C standard retains this bias.
> Simply that it came from the fact that the index value of an array
> was really the offset of the specific item from the start of the array.

In order to reference element a[i],
the computer must first calculate its address.
If you use a [one-based] index,
the compiler would be obliged to calculate

(a + i - 1)

Today, good optimizing C compilers
would eliminate the superfluous subtraction
but, when K & R were designing C,
compilers usually didn't have the resources
(fast processors and large memories)
required to perform such optimizations.

Martin Ambuhl
Guest
Posts: n/a

 08-16-2004
Does It Matter wrote:

> On the other hand, C language originated on a PDP-11. The PDP-11 assembly
> language just uses a fixed source and destination for things like
> In other words, there is not address+offset mode like the C68000 or more
> modern processors.

This is just silly. Please check the eight addressing modes in the
PDP-11 before posting more (just barely topical) "information."

Joe Wright
Guest
Posts: n/a

 08-16-2004
kapilk wrote:
> Sir,
>
> I know that the array index starts in C from 0 and not 1 can any
> body pls. tell me the reason.
>
> Is it because in the subscript i can have a unsigned integer and
> these start from 0
>
> Thanks

Because I like it that way! But really, it's hard to say.

IBM was the first major OEM disk drive maker. IBM numbers tracks
from 0 and sectors from 1. Why? Seagate, Western Digital, Maxtor,
etc. do the same. Why?

Bytes in a record are numbered from 0 while columns on a punch card
number from 1. Go figure.
--
Joe Wright (E-Mail Removed)
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

Dan Pop
Guest
Posts: n/a

 08-17-2004
In <(E-Mail Removed) > http://www.velocityreviews.com/forums/(E-Mail Removed) (kapilk) writes:

> I know that the array index starts in C from 0 and not 1 can any
>body pls. tell me the reason.

Because the language designers decided to make array[i] an alternate
notation for *(array + i). They could have chosen to make array[i]
an alternate notation for *(array + i - 1), in which case array
indices would have been 1-based, but they didn't.

I don't know if this is an original C feature or merely inherited from
one of its predecessors (CPL, BCPL, B).

To someone with a solid assembly background, 0-based indexing appears as
the most natural option, because this is how indexed addressing modes
work on most processors supporting them. And the processor for which
C was originally designed was no exception.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)

Dan Pop
Guest
Posts: n/a

 08-17-2004
In <cfq6sn\$q5u\$(E-Mail Removed)2surf.net> "Allan Bruce" <(E-Mail Removed)> writes:

>if the accesses were from 1, then this would add extra computation and
>therefore be slower. Also, almost every programming language adopts 0 as
>the initial index.

The most popular languages at the time C was designed used 1-based
indexing: FORTRAN, BASIC, Pascal.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)

Richard Tobin
Guest
Posts: n/a

 08-17-2004
In article <cfq6sn\$q5u\$(E-Mail Removed)2surf.net>,
Allan Bruce <(E-Mail Removed)> wrote:

>I think it is due to the way that the compilers work. If you have an array
>of sometype then the way to access these uses the notation
>
>
>if the accesses were from 1, then this would add extra computation and
>therefore be slower.

Only if the compilers were particularly stupid.

Real compilers would just produce

(addressOfStartOfArray - sizeof(sometype)) + (index * sizeof(sometype))

where the first parenthesized expression is known at compile time.

C arrays start at zero because it's The Right Thing to do.

-- Richard

boa
Guest
Posts: n/a

 08-17-2004
Richard Tobin wrote:

> In article <cfq6sn\$q5u\$(E-Mail Removed)2surf.net>,
> Allan Bruce <(E-Mail Removed)> wrote:
>
>
>>I think it is due to the way that the compilers work. If you have an array
>>of sometype then the way to access these uses the notation
>>
>>
>>if the accesses were from 1, then this would add extra computation and
>>therefore be slower.

>
>
> Only if the compilers were particularly stupid.
>
> Real compilers would just produce
>
> (addressOfStartOfArray - sizeof(sometype)) + (index * sizeof(sometype))
>
> where the first parenthesized expression is known at compile time.

Always? Even when the "array" is a pointer to dynamically allocated memory?

>
> C arrays start at zero because it's The Right Thing to do.

Agreed.

boa@home

Richard Tobin
Guest
Posts: n/a

 08-17-2004
In article <2hqUc.172\$(E-Mail Removed)>,
boa <(E-Mail Removed)> wrote:

>>>
>>>if the accesses were from 1, then this would add extra computation and
>>>therefore be slower.

>> Real compilers would just produce
>>
>> (addressOfStartOfArray - sizeof(sometype)) + (index * sizeof(sometype))
>>
>> where the first parenthesized expression is known at compile time.

>Always? Even when the "array" is a pointer to dynamically allocated memory?

True, I was assuming addressOfStartOfArray was supposed to be a constant.

But in many common cases, other optimizations will remove the
overhead. For example, when looping over the array, the index can be

-- Richard

Keith Thompson
Guest
Posts: n/a

 08-17-2004
(E-Mail Removed) (Dan Pop) writes:
> In <cfq6sn\$q5u\$(E-Mail Removed)2surf.net> "Allan Bruce"
> <(E-Mail Removed)> writes:
>
> >if the accesses were from 1, then this would add extra computation and
> >therefore be slower. Also, almost every programming language adopts 0 as
> >the initial index.

>
> The most popular languages at the time C was designed used 1-based
> indexing: FORTRAN, BASIC, Pascal.

Quibble: Pascal allows arrays to be based however the user specifies.
For example (if I remember the syntax correctly):

type
My_Array = array[37 .. 42] of Integer;

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

kal
Guest
Posts: n/a

 08-18-2004
(E-Mail Removed) (Richard Tobin) wrote in message news:<cftdmd\$1k89\$(E-Mail Removed)>...

> True, I was assuming addressOfStartOfArray was supposed to be a constant.
>
> But in many common cases, other optimizations will remove the
> overhead. For example, when looping over the array, the index can be

Not a satisfactory explanation. Your earlier statement was incorrect.