C Programming

Generic Programming in C

How to do generic programming in C

2 minute read

Generic programming is an important idea in programming. In languages like Java and C++, it can be done easily. In this post, I show how to do it in plain old C. We use the classic stack implementation. First, we implement an int version. This is stack.h: typedef struct { int *elems; int logLength; int allocLengh; } stack; void StackNew(stack *s); void StackDispose(stack *s); void StackPush(stack *s, int value); int StackPop(stack *s); This is stack.