C语言数组下标为何从0开始

来源:互联网 发布:老司机网络意思是什么 编辑:程序博客网 时间:2024/04/27 17:31

        Many other features, besides the type system, were put in C for the C compiler-writer's benefit (and
why not, since C compiler-writers were the chief customers for the first few years). Features of C that
seem to have evolved with the compiler-writer in mind are:
• Arrays start at 0 rather than 1. Most people start counting at 1, rather than zero. Compilerwriters
start with zero because we're used to thinking in terms of offsets. This is sometimes
tough on non-compiler-writers; although a[100] appears in the definition of an array, you'd
better not store any data at a[100], since a[0] to a[99] is the extent of the array.

        说白了就是除了类型系统之外,C的许多特征是为了方便编译器设计者而建立的(为什么不呢?开始几年C语言的主要客户就是哪些编译器设计者).数组下标从0而不是从1开始也是根据编译器设计者的思路而来的,因为偏移量的概念在他们心中已是根深蒂固.但这种设计让非编译器设计者感觉很别扭,他们可能会在一个定义的a[10]的a[10]存储数据,导致有名的一位偏移(one-bit offset)错误.

  java为何下标也从0开始呢,估计是考虑到习惯性问题.C/C++程序员被固化成下标从0开始后,想更改它也是件费劲事.

 
原创粉丝点击