Why the base index of array is zero in c language?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
...or an enumerated-type such as color. Pascal, for example ...
I'd forgotten that. I'm sure, back in the day, I'd thought it was cute, but now I am far less sure.
Anyway, it is an interesting discussion; I'm not sure that there much of an efficiency argument for it, given that you can use the preprocessor to re-base the indexing, and the overhead in doing that isn't much and it is preprocessor overhead rather than run-time overhead. Feels wrong, though.
Prior to that, projects [i](e.g. "Multics" ... yes, "Unix = pun intended") were written very-substantially in assembly language.
Multics had a smaller fraction assembly than Unix. It was written almost entirely in a higher level language than C (abstracted further from the machine behavior and less efficient).
Multics being "prior" to Unix is questionable. Multics was under development and not yet at the level of a usable OS from before the idea of Unix was even conceived until after Unix was a usable OS. So the Multics project was certainly underway long before Unix, but Unix was a usable OS before Multics was.
Both borrowed most of their key concepts from ITS, much of which was written in B. But both Multics and Unix took the idea of limiting the assembler portion of the OS much further than ITS did.
As far as why the first array index is zero, it is in how the C language was defined.
I have an ancient "The C Programming Language" by K&R and right in the chapter about arrays it distinctly says "Array subscripts always start at zero in C, so the elements are ndigit[0], ndigit[1], ..."
Also as said by johnsfine, arrays are constructed to be related to pointers. This is highlighted in what used to be chapter 5, 5.3 Pointers and Arrays where they discuss [0] is related to the first pointer offset in memory, with respect to the array.
It is a convenience that the creators of the language chose, which also has a parallelism with pointer offsets and therefore is an extremely helpful, as well as natural, convenience.
As you can see, the definitions of some other languages use initial array indexes of 1 versus 0.
Multics had a smaller fraction assembly than Unix. It was written almost entirely in a higher level language than C (abstracted further from the machine behavior and less efficient).
Multics being "prior" to Unix is questionable. Multics was under development and not yet at the level of a usable OS from before the idea of Unix was even conceived until after Unix was a usable OS. So the Multics project was certainly underway long before Unix, but Unix was a usable OS before Multics was.
Both borrowed most of their key concepts from ITS, much of which was written in B. But both Multics and Unix took the idea of limiting the assembler portion of the OS much further than ITS did.
True. My recollection of history was much poorer than I thought.
I'd forgotten that. I'm sure, back in the day, I'd thought it was cute, but now I am far less sure.
Anyway, it is an interesting discussion; I'm not sure that there much of an efficiency argument for it, given that you can use the preprocessor to re-base the indexing, and the overhead in doing that isn't much and it is preprocessor overhead rather than run-time overhead. Feels wrong, though.
Enumerated types count-up from zero, but even if they did not, the cost is negligible.
Multics had a smaller fraction assembly than Unix. It was written almost entirely in a higher level language than C (abstracted further from the machine behavior and less efficient).
Multics being "prior" to Unix is questionable. Multics was under development and not yet at the level of a usable OS from before the idea of Unix was even conceived until after Unix was a usable OS. So the Multics project was certainly underway long before Unix, but Unix was a usable OS before Multics was.
Both borrowed most of their key concepts from ITS, much of which was written in B. But both Multics and Unix took the idea of limiting the assembler portion of the OS much further than ITS did.
Actually, the "prior" is, I think, conceptually unquestionable: The Multics development at M.I.T. was a joint project of several companies and the university. AT&T was one of those companies, and Kernighan and Ritchie were, I believe, part of the Multics development team that was removed when AT&T decided to withdraw from the project. They (K&R) were then assigned to the group developing text processing tools for AT&T. (Have you ever wondered why the UNIX base tool kit is so heavy on string and text tools?) To make their life easier, they upgraded the B (or, maybe, it was BCPL) macro set to include variable types, etc., and called the resulting macro set C. Then, based on what they'd learned from the Multics project, they put together a "stripped-down" OS that they called (in an obvious pun) UNIX.
Last edited by PTrenholme; 07-12-2015 at 06:38 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.