Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Why index starts in C from 0 and not 1

 
 
kapilk
Guest
Posts: n/a
 
      08-16-2004
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
 
Reply With Quote
 
 
 
 
Allan Bruce
Guest
Posts: n/a
 
      08-16-2004

"kapilk" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


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

addressOfStartOfArray + (index * sizeof(sometype))

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.

Allan


 
Reply With Quote
 
 
 
 
Marco Parrone
Guest
Posts: n/a
 
      08-16-2004
kapilk on 16 Aug 2004 04:38:08 -0700 writes:

> 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


IMHO no, it was an arbitrary decision, it just made sense that way.

You can think of the index like a value to add to the basic pointer.

#include <stdio.h>

int main (int argc, char *argv[])
{
/* here test is a pointer to the first of these 5 characters.
the five characters are consecutive in memory */
char test [5] = {'t', 'e', 's', 't', '\n'};
printf ("%c == %c\n", test [0], * (test + 0));
printf ("%c == %c\n", test [1], * (test + 1)); /* adding 1 you point
to the next character */
printf ("%c == %c\n", test [2], * (test + 2));
printf ("%c == %c\n", test [3], * (test + 3));
return 0;
}

--
Marco Parrone <(E-Mail Removed)> [0x45070AD6]
 
Reply With Quote
 
Does It Matter
Guest
Posts: n/a
 
      08-16-2004
On Mon, 16 Aug 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


I would suspect it has something to do with the fact that C language is a
language designed to work closely with the hardware architecture and most
assembly languages that has an indexed addressing mode start at zero.

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
assignment (MOV), addition (ADD), subtraction (SUB) and comparison (CMP).
In other words, there is not address+offset mode like the C68000 or more
modern processors.

If you believe this is why C starts at zero you'll have to ask the
question, why does assembly language start at zero? But you'll have to ask
it in an assembly language newsgroup.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Thomas Dickey
Guest
Posts: n/a
 
      08-16-2004
Does It Matter <(E-Mail Removed)> 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
> assignment (MOV), addition (ADD), subtraction (SUB) and comparison (CMP).
> In other words, there is not address+offset mode like the C68000 or more
> modern processors.


That's incorrect (the PDP-11 has 8 addressing modes - including offsets
from a register value).

--
Thomas E. Dickey
http://invisible-island.net
ftp://invisible-island.net
 
Reply With Quote
 
Thomas Dickey
Guest
Posts: n/a
 
      08-16-2004
Does It Matter <(E-Mail Removed)> wrote:

> If you believe this is why C starts at zero you'll have to ask the
> question, why does assembly language start at zero? But you'll have to ask
> it in an assembly language newsgroup.


....or Pascal, or other languages that don't date from 1959.

--
Thomas E. Dickey
http://invisible-island.net
ftp://invisible-island.net
 
Reply With Quote
 
Default User
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



Probably because the array indexing operator is really syntactic sugar
for pointer operations.


ptr[i] == *(ptr + i);


Obviously, when using pointer arithmetic, the first element is at ptr +
0, so the first element when using [] to access it is ptr[0].




Brian Rodenborn
 
Reply With Quote
 
Gordon Burditt
Guest
Posts: n/a
 
      08-16-2004
> 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


My answer to this is that C starting from zero is likely to be influenced
by a lot of *MATHEMATICS* starting from zero.

Also, it is more likely that loading or storing an element of an array can
be accomplished with a single machine instruction if you don't have to
deal with the offset of 1.
Gordon L. Burditt
 
Reply With Quote
 
Thomas Stegen
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
>


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

One never knows though.

--
Thomas.
 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      08-16-2004
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thomas Stegen wrote:

> 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
>>

>
> Maybe because the index value is reallythe 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.

- --
Lew Pitcher
IT Consultant, Enterprise Application Architecture,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFBIQ6FagVFX4UWr64RAu9NAKD0AjpIVqgsBerdAA3Rt3 55FnHdjACfTyUG
293Wn2tpoVhKs4IHcx2PwIY=
=ck5V
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" camelean@shaw.ca ASP .Net 3 02-22-2011 07:06 PM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Instant Messenger starts when Outlook Express 6 starts no matter what is checked! Route9w Computer Support 11 07-08-2004 01:13 PM



Advertisments