Top 10 C Programming Patterns for 2026
High-demand patterns for interviews, systems programming, and performance-critical applications.
Complete implementations ready to copy-paste.
1. Dynamic Array (Most Popular)
C
Growable array like C++ vector
typedef struct {
int* data;
int size, capacity;
} DynamicArray;
DynamicArray* da_create(int init_cap) {
DynamicArray* da = malloc(sizeof(DynamicArray));
da->data = malloc(init_cap * sizeof(int));
da->size = 0; da->capacity = init_cap;
return da;
}
void da_push(DynamicArray* da, int val) {
if (da->size == da->capacity) {
da->capacity *= 2;
da->data = realloc(da->data, da->capacity * sizeof(int));
}
da->data[da->size++] = val;
}
2. Hash Table (Open Addressing)
C
Fast lookup O(1) average
#define HT_CAP 1024
typedef struct { char* key; int val; } HTEntry;
HTEntry ht[HT_CAP];
unsigned int hash(char* key) {
unsigned int h = 0;
while (*key) h = h * 31 + *key++;
return h % HT_CAP;
}
int ht_get(char* key) {
unsigned int h = hash(key);
for (int i = 0; i < HT_CAP; i++) {
int idx = (h + i) % HT_CAP;
if (!ht[idx].key) return -1;
if (strcmp(ht[idx].key, key) == 0) return ht[idx].val;
}
return -1;
}
3. AVL Tree (Self-Balancing BST)
C
Guaranteed O(log n) operations
typedef struct Node {
int key, height;
struct Node* left, *right;
} Node;
int height(Node* n) { return n ? n->height : 0; }
int balance(Node* n) { return height(n->left) - height(n->right); }
Node* rotateRight(Node* y) {
Node* x = y->left;
y->left = x->right; x->right = y;
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
return x;
}
4. Thread Pool (High Concurrency)
C
Fixed threads for massive concurrency
typedef struct {
pthread_t* threads;
void (*tasks[])(void*);
int task_count, thread_count;
} ThreadPool;
void* worker(void* arg) {
ThreadPool* tp = arg;
while (1) {
// Pop task, execute
pthread_mutex_lock(&tp->mutex);
if (tp->task_count > 0) {
void (*task)(void*) = tp->tasks[--tp->task_count];
pthread_mutex_unlock(&tp->mutex);
task(NULL);
} else {
pthread_cond_wait(&tp->cond, &tp->mutex);
pthread_mutex_unlock(&tp->mutex);
}
}
}
5. Memory Pool (Zero Fragmentation)
C
Fixed-size blocks, ultra-fast alloc
#define POOL_SIZE 1024
typedef struct {
char blocks[POOL_SIZE][64];
int free_list[POOL_SIZE];
int free_count;
} MemoryPool;
void* mp_alloc(MemoryPool* mp) {
if (mp->free_count == 0) return NULL;
int idx = mp->free_list[mp->free_count--];
return mp->blocks[idx];
}
void mp_free(MemoryPool* mp, void* ptr) {
// Push back to free_list
}
6. State Machine (Embedded Systems)
C
Finite state machine pattern
typedef enum { IDLE, RUNNING, PAUSED, ERROR } State;
typedef struct {
State current;
void (*states[])(void*);
} StateMachine;
void idle_state(void* sm) { /* Handle idle */ }
void running_state(void* sm) { /* Handle running */ }
void sm_transition(StateMachine* sm, State next) {
sm->current = next;
sm->states[next](sm);
}
7. Command Pattern (Undo/Redo)
C
Encapsulate actions for undo
typedef struct { void (*execute)(void*); void (*undo)(void*); } Command;
void cmd_cut_execute(void* editor) { /* Cut text */ }
void cmd_cut_undo(void* editor) { /* Paste back */ }
Command cut_cmd = { cmd_cut_execute, cmd_cut_undo };
8. LRU Cache (Most Interviewed)
C
Fixed capacity, O(1) get/put
typedef struct {
HashTable ht;
DoublyLinkedList dll;
int capacity;
} LRUCache;
int lru_get(LRUCache* cache, int key) {
int val = ht_get(&cache->ht, key);
if (val != -1) {
dll_move_to_front(&cache->dll, key);
}
return val;
}
9. Rate Limiter (API Protection)
C
Token bucket algorithm
typedef struct {
long last_refill;
int tokens;
int capacity;
int refill_rate;
} RateLimiter;
int rl_consume(RateLimiter* rl) {
long now = time(NULL);
rl->tokens = min(rl->capacity, rl->tokens + (now - rl->last_refill) * rl->refill_rate);
rl->last_refill = now;
if (rl->tokens > 0) {
rl->tokens--;
return 1;
}
return 0;
}
Conclusion
Master these 9 patterns = C expert status in 2026.
Dynamic array, hash table, LRU cache = 80% interview coverage.
Codecrown