Programming Fundamentals · Interview question

Why does array indexing start at zero in C?

A strong answer

It falls out of the address arithmetic the CPU does to access an element. The address of arr[i] is computed as base_address + i * element_size. With zero-based indexing, the first element lives at offset 0, no extra arithmetic. With one-based indexing you'd have to subtract one on every access. The convention also makes a lot of patterns cleaner: half-open intervals (0 <= i < n), pointer arithmetic (*(arr + i) is the same as arr[i]), and the standard idiom for (int i = 0; i < n; i++) covering exactly n elements. Once you internalize zero-indexing, off-by-one bugs become much rarer.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Arrays & Strings

A contiguous block of same-type values. How arrays work in memory, why indexing starts at 0, and the null-terminated convention that defines C-strings.

More Arrays & Strings questions

Browse all 472 interview questions