Home >Backend Development >C++ >Why do C/C++ array indexes start from zero?

Why do C/C++ array indexes start from zero?

王林
王林forward
2023-09-10 19:25:021461browse

Why do C/C++ array indexes start from zero?

Since the array index starts from 0, a[i] can be implemented as *(a i).

If the array index starts from 1, then a[i] will be implemented as *(a i-1), which will consume more time during the compilation process, and the performance of the program will also be affected.

Therefore, it is better to index the array starting from 0.

Given a simple array program -

Sample code

int main() {
   int array[5] = {7, 7, 7, 6, 6};
   for (int i = 0; i < 5; i++)
      cout << *(array + i);
   return 0;
}

Output

7 7 7 6 6

The above is the detailed content of Why do C/C++ array indexes start from zero?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete