C Hash Functions

Intermediate
6 min read
Updated: Nov 2026
1 example

Turning Keys into Numbers

A good hash function is fast, deterministic, and spreads keys evenly. A character-sum hash is simple but sends anagrams to the same slot. A polynomial (multiply-and-add) hash such as h = h * 31 + c, or djb2 (h = h * 33 + c starting at 5381), mixes positions into the result and distributes strings far better. Use unsigned arithmetic so overflow is well defined.

Remember
unsigned long h = 5381;               /* djb2 */
while (*s) h = h * 33 + (unsigned char)*s++;
slot = h % TABLE_SIZE;

Cheat Sheet

Keep these facts handy while you work with Hash Functions in C.

TopicDetail
DeterministicSame key, same hash
UniformSpreads keys evenly
UnsignedWraps safely on overflow
StringsMultiply-and-add hashes

C Examples

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

Example 1 — Sum hash versus polynomial hash

"cat" and "act" are anagrams; the table has 11 slots.

C
#include <stdio.h>

static unsigned sum_hash(const char *s, unsigned m) {
    unsigned h = 0;
    while (*s) h += (unsigned char)*s++;
    return h % m;
}

static unsigned poly_hash(const char *s, unsigned m) {
    unsigned long h = 0;
    while (*s) h = h * 31 + (unsigned char)*s++;
    return (unsigned)(h % m);
}

int main(void) {
    const char *words[] = {"cat", "act", "dog"};
    for (int i = 0; i < 3; i++)
        printf("%s: sum=%u poly=%u\n", words[i],
               sum_hash(words[i], 11), poly_hash(words[i], 11));
    return 0;
}

How It Works

  1. "cat" and "act" have the same letters, so the sum hash gives both slot 4.
  2. The polynomial hash depends on position and separates them (10 and 9).
  3. Both reduce the hash with % 11.

Edge Cases & Pitfalls

Check these before relying on Hash Functions in real programs.

sign

Signed overflow

Overflowing a signed int is undefined behavior. Use unsigned types.

len

Weak string hashes

Using only length or first character causes heavy clustering.

Key Takeaways

  • Be deterministic and uniform.
  • Mix character positions.
  • Prefer unsigned math.

One line: A good hash function is fast, deterministic, and spreads keys evenly.

Frequently Asked Questions

It is fast to compute, deterministic, and distributes keys uniformly across the table.
A popular string hash that starts at 5381 and computes h = h * 33 + c for each character.
Continue with "C Hash Table Implementation" next. Design a table with put, get, and destroy using chained buckets. The C Hash Tables hub lists every topic in order.

Next: Hash Table Implementation

Design a table with put, get, and destroy using chained buckets.

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