Hash Table Insertion and Searching in C

Intermediate
6 min read
Updated: Nov 2026
1 example

Insert, Update, and Find

Insertion hashes the key, scans the chain for an existing entry (update) and otherwise adds a new one. Search hashes again and walks the same chain. String keys need strcmp for equality and must be copied into the table if the original may change. Both operations are O(1) on average and O(chain length) worst case.

Remember
unsigned i = hash(word) % B;
for (n = t[i]; n; n = n->next)
    if (strcmp(n->word, word) == 0) { n->count++; return; }
/* else allocate, copy word, link in front */

Cheat Sheet

Keep these facts handy while you work with Hash Table Insertion and Searching in C in C.

TopicDetail
Equalitystrcmp(a, b) == 0
Copy keysmalloc + strcpy
AverageO(1)
WorstO(n) if all collide

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Hash Table Deletion in C.

Example 1 — Word frequency counter

Count the words in "to be or not to be".

C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define B 8

struct Node { char *word; int count; struct Node *next; };

static unsigned hash(const char *s) {
    unsigned h = 5381;
    while (*s) h = h * 33 + (unsigned char)*s++;
    return h % B;
}

static void add(struct Node *t[], const char *w) {
    unsigned i = hash(w);
    for (struct Node *n = t[i]; n; n = n->next)
        if (strcmp(n->word, w) == 0) { n->count++; return; }
    struct Node *n = malloc(sizeof *n);
    if (!n) exit(1);
    n->word = malloc(strlen(w) + 1);
    if (!n->word) exit(1);
    strcpy(n->word, w);
    n->count = 1;
    n->next = t[i];
    t[i] = n;
}

static int count(struct Node *t[], const char *w) {
    for (struct Node *n = t[hash(w)]; n; n = n->next)
        if (strcmp(n->word, w) == 0) return n->count;
    return 0;
}

int main(void) {
    struct Node *table[B] = {0};
    const char *text[] = {"to", "be", "or", "not", "to", "be"};
    const char *ask[] = {"to", "be", "or", "not", "maybe"};

    for (int i = 0; i < 6; i++) add(table, text[i]);
    for (int i = 0; i < 5; i++) printf("%s: %d\n", ask[i], count(table, ask[i]));

    for (int i = 0; i < B; i++) {
        struct Node *n = table[i];
        while (n) { struct Node *nx = n->next; free(n->word); free(n); n = nx; }
    }
    return 0;
}

How It Works

  1. A repeated word finds its existing node and increments the count.
  2. A new word is copied with malloc + strcpy and linked into its bucket.
  3. Searching a missing word returns 0.

Edge Cases & Pitfalls

Check these before relying on Hash Table Insertion and Searching in C in real programs.

alias

Storing the caller's pointer

If you store w itself and the caller reuses the buffer, your key changes. Copy it.

cmp

Comparing pointers

Use strcmp, not ==, to compare strings.

Key Takeaways

  • Hash, scan, update or insert.
  • Copy string keys.
  • Compare with strcmp.

One line: Insertion hashes the key, scans the chain for an existing entry (update) and otherwise adds a new one.

Frequently Asked Questions

Compute the hash, go to that slot, and compare keys along the chain (or probe sequence) until you find the key or an empty slot.
The caller may modify or free its buffer, which would corrupt keys stored by pointer.
Continue with "Hash Table Deletion in C" next. Unlink an entry from its chain and free it. The C Hash Tables hub lists every topic in order.

Next: Hash Table Deletion in C

Unlink an entry from its chain and free it.

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.

11 people found this page helpful