Linear Search Program in C Explained with Example (2025)

Written By: Nathan Kellert

Posted On:

Hey there! So you’re just starting out with C programming or revising some basics? Well, one of the simplest and most important algorithms to learn is linear search. It’s super beginner-friendly, easy to understand, and often asked in interviews or coding tests. Let’s walk through what it is, how it works, and see a basic linear search program in C.

Alright, imagine you’ve got a list of numbers and you’re trying to find a specific number in that list. What do you do? You check one by one, from start to end, until you either find it or finish the list. That’s exactly what linear search does.

It’s also called sequential search because you go through the array one element at a time.

  • When the list is small
  • When the list isn’t sorted
  • When you need something quick and simple

It’s not the most efficient method for large data, but it works fine for small or unsorted lists.

Logic Behind Linear Search in C

  1. Take the array and the number to search (called the target).
  2. Loop through each element in the array.
  3. If the current element matches the target, return the index (or say “found”).
  4. If you reach the end and didn’t find it, say “not found”.

Simple, right?

Linear Search Program in C

#include <stdio.h>

int main() {
    int arr[100], n, target, i, found = 0;

    printf("Enter number of elements: ");
    scanf("%d", &n);

    printf("Enter %d elements:\n", n);
    for(i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    printf("Enter the number to search: ");
    scanf("%d", &target);

    for(i = 0; i < n; i++) {
        if(arr[i] == target) {
            printf("Element found at index %d (position %d)\n", i, i+1);
            found = 1;
            break;
        }
    }

    if(!found) {
        printf("Element not found in the array.\n");
    }

    return 0;
}

Output Example

Enter number of elements: 5  
Enter 5 elements:  
3 7 9 1 5  
Enter the number to search: 9  
Element found at index 2 (position 3)

What If the Number Isn’t in the Array?

The code above handles that too. If the loop ends without finding the number, it simply prints “Element not found in the array.”

  • Best Case – O(1) → if the element is at the start
  • Worst Case – O(n) → if the element is at the end or not there at all
  • Average Case – O(n/2), but we still say O(n)

So yeah, it’s not super fast, but it’s very easy and works fine in simple use-cases.

Pros and Cons

Pros:

  • Very simple to implement
  • No need to sort the array
  • Works on any kind of list

Cons:

  • Slow for large data
  • Not efficient if repeated searches are required

Wrapping Up

Linear search is one of those must-know basic algorithms that help you understand how searching works in programming. Even if it’s not the best in terms of speed, it’s great for learning and quick solutions. Once you’re comfortable with linear search, you can move on to binary search, which is faster but requires a sorted array.

Need help with binary search or want a version that works with strings or characters? Just give me a shout!

Photo of author

Nathan Kellert

Nathan Kellert is a skilled coder with a passion for solving complex computer coding and technical issues. He leverages his expertise to create innovative solutions and troubleshoot challenges efficiently.

Leave a Comment