LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-06-2015, 05:01 AM   #1
findnerd2
LQ Newbie
 
Registered: May 2015
Location: India
Posts: 11

Rep: Reputation: 0
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.
 
Old 07-06-2015, 05:09 AM   #2
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
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.
 
Old 07-06-2015, 06:10 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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.
 
1 members found this post helpful.
Old 07-06-2015, 06:43 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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.
 
Old 07-06-2015, 06:47 AM   #5
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by findnerd2 View Post
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.

Best regards,
HMW
 
Old 07-06-2015, 07:22 AM   #6
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
Quote:
Originally Posted by HMW View Post
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.

Best regards,
HMW
I seem to recall that something like Visual Basic had an option to start arrays at 1 but my google-fu fails me on this one.
 
Old 07-06-2015, 08:09 AM   #7
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
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
 
Old 07-06-2015, 08:37 AM   #8
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Quote:
Originally Posted by HMW View Post
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.
 
Old 07-06-2015, 08:37 AM   #9
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by HMW View Post
I haven't ever stumbled upon a language that do NOT start at zero when handling arrays.
Copied from: https://en.wikipedia.org/wiki/Zero-based_numbering
Code:
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.
APL allows each program to specify 0 or 1 as the index origin. See: http://microapl.com/apl_help/ch_020_070_380.htm

Daniel B. Martin

Last edited by danielbmartin; 07-06-2015 at 08:38 AM. Reason: Cosmetic improvement
 
1 members found this post helpful.
Old 07-06-2015, 10:10 AM   #10
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by HMW View Post
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.

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.
 
Old 07-06-2015, 11:50 AM   #11
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by suicidaleggroll View Post
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.
There you go. There are always exceptions!
 
Old 07-06-2015, 12:53 PM   #12
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by sundialsvcs View Post
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.
 
Old 07-06-2015, 01:10 PM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
just for fun, here is how perl handles: http://perldoc.perl.org/perlvar.html#%24[ (see the bottom of the page)
 
Old 07-06-2015, 01:27 PM   #14
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Quote:
Originally Posted by HMW View Post
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 ...
 
Old 07-06-2015, 01:39 PM   #15
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Quote:
Originally Posted by metaschima View Post
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.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] use array index as file name rbees Programming 23 03-27-2015 07:58 AM
[SOLVED] How to find the index of bash array jags1984 Linux - Newbie 2 01-20-2014 12:06 AM
Decimal in array index micyew Programming 9 07-10-2012 09:28 AM
Perl: printing array's index and value craig467 Programming 8 08-30-2006 03:50 PM
index of an element in the array ? thelonius Programming 1 09-24-2005 12:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration