C Example: Ring Buffer: fixed-capacity queue that overwrites the oldest item

🗓 Last updated: 2026-11-30👤 Written by: Gray✓ Reviewed by: MOTO🏷 Category: Data Structures

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.

⚡ Intermediate Reference entries: kw-structkw-intfn-printf
🖥 Run OnlineGCC 10.2 · C11 standard · click ▶ to run · Copy code
🖨 Reference OutputMatches the online run in a standard C11 environment
C · GCC 10.2 · -std=c11
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

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏