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.
Why the base index of array is zero in c language?
I was trying to work with array in c language and wondered that the base index of array does not start with other than zero like it doesn’t start with 1, 2, 3.. I just want to know the concept behind.
“Why the base index of array is zero in c language” As I am a beginner in c programming language and I have more doubts about C issues, I will definitely post and look for more c programming questions and answers in this community forum. Hope I will get genuine replies to my programming query.
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680
Rep:
I don't know for certain but I would think it could be that counters always start at 0 not 1. That goes for anything from stopwatches to ripple counters meaning that in computing counting starts at 0. Since computing starts counting at 0 so does C.
Of course, it could just as easily be for aesthetic reasons.
One design feature of C makes arrays and pointers look as much as possible like the same thing, so x[n] and *(x+n) mean the same thing, regardless of whether x is an array or a pointer. Obviously, you want *x and *(x+0) to mean the same, which would be very confusing is the first element of arrays were not index 0.
Think like a CPU chip. When you are using "indexed addressing," an offset taken from an appropriate hardware register is added to the address. This is exactly what "C" is doing when referencing an array or taking an offset from a pointer. (The latter being "indexed indirect addressing.") The minimum offset is zero.
"C" is a very low-level language designed to be "one step above assembler," and in fact to incorporate an assembler in the form of the asm{...} block. All of the constructs in "C" correspond more-or-less directly to hardware concepts and to machine instructions. This is by design.
Last edited by sundialsvcs; 07-06-2015 at 06:47 AM.
I was trying to work with array in c language and wondered that the base index of array does not start with other than zero like it doesn’t start with 1, 2, 3
Hi!
I haven't ever stumbled upon a language that do NOT start at zero when handling arrays. In other words, it's not a "C" thing.
because addressing
C is somewhat close to how the cpu does that
an array is a piece of memory pointed to by the address associated with its name
with its size being num_elements*sizeof(element)
ex: type array[num_elements]
the first element is right at that "array" address
to calculate the address of any element the cpu would do
array+element_num*sizeof(element)
so if element_num were to be at least 1 it would mean that either the first element was skipped or that the compiler would automatically subtract 1 from element_num
I haven't ever stumbled upon a language that do NOT start at zero when handling arrays. In other words, it's not a "C" thing.
In Lua, arrays are implemented by indexing tables with integers. Due to that, an array in Lua can have any integer as base index, but common practice in Lua is indeed to have 1 as base index for arrays.
Other programming languages, such as Fortran or COBOL have
array subscripts starting with one, because they were meant as
high-level programming languages, and as such they had to have a
correspondence to the usual ordinal numbers. Some recent languages,
such as Lua, have adopted the same convention for the same reason.
I haven't ever stumbled upon a language that do NOT start at zero when handling arrays. In other words, it's not a "C" thing.
Best regards,
HMW
Fortran and Matlab both start at 1. Although with Fortran that's just the default, you can force it to start at 0 if you want, you can even force it to start at -3 or +7 if you want.
Fortran and Matlab both start at 1. Although with Fortran that's just the default, you can force it to start at 0 if you want, you can even force it to start at -3 or +7 if you want.
Think like a CPU chip. When you are using "indexed addressing," an offset taken from an appropriate hardware register is added to the address. This is exactly what "C" is doing when referencing an array or taking an offset from a pointer. (The latter being "indexed indirect addressing.") The minimum offset is zero.
"C" is a very low-level language designed to be "one step above assembler," and in fact to incorporate an assembler in the form of the asm{...} block. All of the constructs in "C" correspond more-or-less directly to hardware concepts and to machine instructions. This is by design.
I agree, but I have seen in books that C is a high level language. Honestly, I think it is one of the few mid-level languages, where you can use inline assembly (low level), and create functions and structs (high level). It is designed for efficiency, so counters have to start a 0.
I haven't ever stumbled upon a language that do NOT start at zero when handling arrays. In other words, it's not a "C" thing.
Many languages allow array-indexes to be a subrange (that does not have to start at zero), or an enumerated-type such as color. Pascal, for example ...
I agree, but I have seen in books that C is a high level language. Honestly, I think it is one of the few mid-level languages, where you can use inline assembly (low level), and create functions and structs (high level). It is designed for efficiency, so counters have to start a 0.
"C" is a high-level language in the sense that it provides hardware abstraction. Most of the Linux operating system (like the Unix operating system before it ...) is written in "C," with an accompanying /arch directory containing the architecture-specific stuff. The "C" language is designed to provide hardware abstraction, while remaining very close to, shall we say, "what the hardware can do, and the way that the hardware actually does it."
The Unix operating system was quite revolutionary, in its day, for having been programmed in "C," which was a language basically devised for that purpose. Prior to that, projects (e.g. "Multics" ... yes, "Unix = pun intended") were written very-substantially in assembly language. (That is to say, in t-h-e assembly language of t-h-e (only) model of computer that the operating system in question was designed to run on.) Unix was therefore the first operating system to be called, "portable."
The "C" language, by design and intention, has to be "very(!) broadly useful." It must be able to write "user-land" programs, in which glibc is presumed to exist, and "kernel-land" programs (device drivers and such, even the Linux dispatcher ...) in which nothing can be presumed to exist. It is certainly a testament to "C"'s design that most of the Linux system's source-code does not reside in any /arch directory. It is also a testament, both to the "C" language and specifically to the gcc/glibc implementation thereof, that the Linux operating system today runs on more than twenty radically-different hardware architectures.
Last edited by sundialsvcs; 07-07-2015 at 10:43 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.