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

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

王林
Release: 2023-09-10 19:25:02
forward
1427 people have browsed it

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;
}
Copy after login

Output

7 7 7 6 6
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template