C Linear Search

Intermediate
6 min read
Updated: Nov 2026
1 example

Checking One by One

Linear (sequential) search compares the key with each element in turn. It needs no sorting, works on arrays and linked lists, and is O(n) worst case. For short or unsorted data it is often the best choice. Small variations find the last match, count matches, or stop early.

Remember
for (int i = 0; i < n; i++)
    if (a[i] == key) return i;
return -1;

Cheat Sheet

Keep these facts handy while you work with Linear Search in C.

TopicDetail
TimeO(n)
SpaceO(1)
RequiresNothing (unsorted OK)
VariantsFirst, last, count

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Binary Search.

Example 1 — First, last, count, and miss

The array contains 2 twice.

C
#include <stdio.h>

int main(void) {
    int a[] = {4, 2, 7, 2, 9};
    int n = 5, key = 2;
    int first = -1, last = -1, count = 0;

    for (int i = 0; i < n; i++) {
        if (a[i] == key) {
            if (first < 0) first = i;
            last = i;
            count++;
        }
    }
    printf("first %d at index %d\n", key, first);
    printf("last %d at index %d\n", key, last);
    printf("occurrences of %d: %d\n", key, count);

    int missing = 5, pos = -1;
    for (int i = 0; i < n; i++) if (a[i] == missing) { pos = i; break; }
    printf("search %d: %d\n", missing, pos);
    return 0;
}

How It Works

  1. One pass tracks the first match, the last match, and the count.
  2. The final loop stops at the first match using break.
  3. A missing key leaves the result at -1 after scanning everything.

Edge Cases & Pitfalls

Check these before relying on Linear Search in real programs.

early

Forgetting to stop

Without break or return, the loop scans the rest of the array needlessly.

type

Comparing strings with ==

For strings use strcmp, not ==.

Key Takeaways

  • Simple and general.
  • O(n) time.
  • Good for small or unsorted data.

One line: Linear (sequential) search compares the key with each element in turn.

Frequently Asked Questions

When the data is unsorted, small, or you search only once, so sorting would cost more than it saves.
O(n) in the worst and average cases, and O(1) in the best case.
Continue with "C Binary Search" next. Trace how binary search narrows the range in O(log n). The C Searching Algorithms hub lists every topic in order.

Next: Binary Search

Trace how binary search narrows the range in O(log n).

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful