Skip to main content
C Bug Hunt Medium +20 XP

Off-by-One in Loop

This loop writes past the array boundary.

CFind the bug
1int arr[5];
2for (int i = 0; i <= 5; i++) arr[i] = i;
The bug is on line 2

Select the correct fix

Show answer and explanation

Answer: Change i <= 5 to i < 5

arr has indices 0-4. When i=5, arr[5] is out of bounds — classic buffer overflow off-by-one error.

Hint: Valid indices for arr[5] are 0 to 4.

What this challenge tests

Bug Hunt in C: arrays, bounds, off-by-one.

Related challenges