Why index starts from 0? Why not 1? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why index starts from 0? Why not 1?

18th Oct 2017, 2:15 PM
Tejinderpal Singh
Tejinderpal Singh - avatar
4 Answers
+ 3
Indexes are an offset from the first position. If you are 0 positions away from the start, you are at the first item. Whether or not there is an item there (which you could count) is not related to the offset. Starting at 1 makes it easier to confuse 'count of items' with 'offset to item'.
18th Oct 2017, 2:27 PM
Kirk Schafer
Kirk Schafer - avatar
+ 3
First answer, "historical reasons": https://stackoverflow.com/a/22547440 "Translation: They don't want to write all new plot, print, etc. routines." Next answer down: Because people using Matlab aren't necessarily programmers... 0 vs 1 is a source of bugs here. (Some programming languages tried to be more approachable in the same way, opinions vary) In my opinion, programmers have the additional responsibility to go beyond 'catering to the people'. Unfortunately this means tracking who went with popular (the politically correct: 1) vs. accurate (this is how distance works: 0).
18th Oct 2017, 2:59 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
because index is the distance from first character.
18th Oct 2017, 3:43 PM
shobhit
shobhit - avatar
0
But the index starts from 1 in MATLAB
18th Oct 2017, 2:35 PM
Tejinderpal Singh
Tejinderpal Singh - avatar