C Program to find the index of a given number in an array
Learn how to find the zero-based index of a given number in a C array using linear search, with validated code, examples, and a dry run.
Every element in a C array has a zero-based index. The first element is at index 0, the second is at index 1, and the last element is at index size - 1.
To find the index of a given number, compare the target with each array element from left to right. When a match is found, save the current loop index.
For example, in the array:
14 6 27 6 9
the first occurrence of 6 is at index 1.
C Program to Find the Index of a Number in an Array
#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
int array[MAX_SIZE];
int size;
int target;
int foundIndex = -1;
printf("Enter the number of elements: ");
if (scanf("%d", &size) != 1 || size < 1 || size > MAX_SIZE) {
printf("Please enter a size between 1 and %d.\n", MAX_SIZE);
return 1;
}
printf("Enter %d elements:\n", size);
for (int index = 0; index < size; index++) {
if (scanf("%d", &array[index]) != 1) {
printf("Invalid array element.\n");
return 1;
}
}
printf("Enter the number to find: ");
if (scanf("%d", &target) != 1) {
printf("Invalid number.\n");
return 1;
}
for (int index = 0; index < size; index++) {
if (array[index] == target) {
foundIndex = index;
break;
}
}
if (foundIndex == -1) {
printf("%d does not exist in the array.\n", target);
} else {
printf("Index of %d = %d\n", target, foundIndex);
}
return 0;
}
Sample Output
Enter the number of elements: 5
Enter 5 elements:
14 6 27 6 9
Enter the number to find: 6
Index of 6 = 1
The result is 1, not 2, because C array indices begin at zero.
How the Program Works
foundIndexis initialized to-1. Since valid array indices start at0,-1safely represents “not found.”- The program reads the array and the target number.
- The search loop compares
array[index]withtarget. - When the values are equal, the current index is stored in
foundIndex. breakstops the loop after the first match.- If
foundIndexremains-1, the number is absent; otherwise, it contains the target's index.
Here is a dry run for target 6:
| Index | Array element | Comparison | Result |
|---|---|---|---|
| 0 | 14 | 14 != 6 | Continue |
| 1 | 6 | 6 == 6 | Save index 1 and stop |
Elements after index 1 are not examined because the program only needs the first matching index.
Index Versus Position
An index and a position are related but not identical:
| Element | Index | Position |
|---|---|---|
| 14 | 0 | 1 |
| 6 | 1 | 2 |
| 27 | 2 | 3 |
The conversion is:
position = index + 1
This program prints the index, so it reports 1 for the second element.
What If the Number Appears More Than Once?
The sample array contains 6 at indices 1 and 3. Because the loop searches from left to right and uses break, it returns index 1, the first occurrence.
To print every matching index, the program would omit break and print the current index each time array[index] == target. For the sample array, that would produce indices 1 and 3.
What If the Number Does Not Exist?
If no element matches the target, foundIndex remains -1:
Enter the number of elements: 4
Enter 4 elements:
5 12 8 20
Enter the number to find: 7
7 does not exist in the array.
Using -1 is convenient because it cannot be confused with any valid index.
Why Linear Search?
Linear search works with unsorted arrays and does not require any preprocessing. It checks elements in order until a match is found or the array ends.
A sorted array can sometimes be searched faster with binary search, but sorting an unsorted array only to perform one search may require more work and can change the original element positions.
Time and Space Complexity
- Best-case time complexity:
O(1)when the target is the first element. - Worst-case time complexity:
O(n)when the target is last or absent. - Extra space complexity:
O(1)because the search uses only the target, an index result, and a loop variable.
Common Mistakes
- Printing
index + 1when the problem asks for the zero-based index. - Initializing
foundIndexto0; zero is a valid index and cannot also mean “not found.” - Forgetting
breakwhen only the first occurrence is required. - Using
=instead of==in the comparison. - Looping while
index <= size, which accesses one position beyond the array. The correct condition isindex < size. - Assuming the array is sorted and using binary search without verifying that requirement.
Linear search provides a simple and reliable way to find the first index of a given number in any array.