0
0
CProgramBeginner · 2 min read

C Program to Search Element in Array

To search an element in an array in C, use a loop to check each element with if (arr[i] == key) and print the index if found.
📋

Examples

InputArray: [1, 2, 3, 4, 5], Search: 3
OutputElement 3 found at index 2
InputArray: [10, 20, 30, 40, 50], Search: 25
OutputElement 25 not found in the array
InputArray: [5], Search: 5
OutputElement 5 found at index 0
🧠

How to Think About It

To find an element in an array, start from the first item and compare it with the target value. Move to the next item if it does not match. Repeat until you find the element or reach the end of the array.
📐

Algorithm

1
Get the size of the array and the element to search.
2
Loop through each element of the array from start to end.
3
Compare the current element with the search element.
4
If they match, print the index and stop searching.
5
If the loop ends without a match, print that the element is not found.
💻

Code

c
#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = 5, key = 3, found = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] == key) {
            printf("Element %d found at index %d\n", key, i);
            found = 1;
            break;
        }
    }
    if (!found) {
        printf("Element %d not found in the array\n", key);
    }
    return 0;
}
Output
Element 3 found at index 2
🔍

Dry Run

Let's trace searching for element 3 in array [1, 2, 3, 4, 5].

1

Start loop with i=0

Compare arr[0] = 1 with key = 3 (not equal)

2

Next loop with i=1

Compare arr[1] = 2 with key = 3 (not equal)

3

Next loop with i=2

Compare arr[2] = 3 with key = 3 (equal, found)

4

Print result and exit loop

Output: Element 3 found at index 2

Index (i)Array ValueComparison Result
01No match
12No match
23Match found
💡

Why This Works

Step 1: Loop through array

The program checks each element one by one using a for loop.

Step 2: Compare elements

Inside the loop, it uses if (arr[i] == key) to find a match.

Step 3: Print and stop

When a match is found, it prints the index and stops searching with break.

Step 4: Handle not found

If no match is found after the loop, it prints that the element is not in the array.

🔄

Alternative Approaches

Using a function to search
c
#include <stdio.h>

int search(int arr[], int n, int key) {
    for (int i = 0; i < n; i++) {
        if (arr[i] == key) return i;
    }
    return -1;
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = 5, key = 3;
    int index = search(arr, n, key);
    if (index != -1)
        printf("Element %d found at index %d\n", key, index);
    else
        printf("Element %d not found in the array\n", key);
    return 0;
}
This approach separates searching logic into a reusable function, improving code organization.
Linear search without break
c
#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = 5, key = 3, found = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] == key) {
            printf("Element %d found at index %d\n", key, i);
            found = 1;
        }
    }
    if (!found) {
        printf("Element %d not found in the array\n", key);
    }
    return 0;
}
This finds all occurrences but is less efficient if only the first match is needed.

Complexity: O(n) time, O(1) space

Time Complexity

The program checks each element once in the worst case, so time grows linearly with array size.

Space Complexity

No extra space is used except a few variables, so space is constant.

Which Approach is Fastest?

Linear search with break is fastest for unsorted arrays; binary search is faster but requires sorted arrays.

ApproachTimeSpaceBest For
Linear search with breakO(n)O(1)Unsorted arrays, stop at first match
Linear search without breakO(n)O(1)Find all matches
Function-based searchO(n)O(1)Reusable code, clarity
💡
Use break to stop searching once the element is found to save time.
⚠️
Forgetting to check all elements or not handling the case when the element is not found.