Why array indexes are zero based in most programming languages?

前端 未结 6 871
说谎
说谎 2020-11-29 13:46

C++, C#, C, D, Java,... are zero based.

Matlab is the only language I know that begin at 1.

6条回答
  •  暖寄归人
    2020-11-29 14:05

    Probably "C" got it because it is more efficient. To calculate address of item in 0-based array it is enough to multiple Index by ItemSize, for 1-based array you have to calculate (Index-1)*ItemSize. "C" and then "C++" where most popular languages, so new languages have to follow same rules, it helps to avoid mistakes for those who use C/C++. But this question seems to be offtopic and i guess it will be closed by moderator.

    P.S. In Delphi/Pascal strings are 1-based, but for arrays you have to provide range and so you can use what you like.

提交回复
热议问题