C Example: Ring Buffer: fixed-capacity queue that overwrites the oldest item
Implements a five-slot ring buffer: head and count describe the logical order; when full, a push overwrites the oldest item and advances head, and a pop takes from head and wraps around. Pushing 1 through 7 shows 1 and 2 being evicted, then pops verify the order.
🖥 Run OnlineGCC 10.2 · C11 standard · click ▶ to run · Copy code
🖨 Reference OutputMatches the online run in a standard C11 environment
push 1 -> buffer(1/5): 1 push 2 -> buffer(2/5): 1 2 push 3 -> buffer(3/5): 1 2 3 push 4 -> buffer(4/5): 1 2 3 4 push 5 -> buffer(5/5): 1 2 3 4 5 push 6 -> buffer(5/5): 2 3 4 5 6 push 7 -> buffer(5/5): 3 4 5 6 7 pop 3 -> buffer(4/5): 4 5 6 7 pop 4 -> buffer(3/5): 5 6 7 pop 5 -> buffer(2/5): 6 7 pop 6 -> buffer(1/5): 7 pop 7 -> buffer(0/5):
🧩 Keep Learning