Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Queues (FIFO) Using Circ | C Programming Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. C Programming Fundamentals

Lesson 32 of 50

·c

Queues (FIFO) Using Circular Buffers

12 mins

Course progress64%

What you'll learn

Keep these ideas in mind as you work through the lesson.

4 points
  • `front` points to the first element; `rear` points to the slot where the next element will be inserted.
  • Modulo operation wraps indices (`% CAP`).
  • Check `size == CAP` for overflow and `size == 0` for underflow.
  • Excellent for producer‑consumer problems.

A queue follows first‑in‑first‑out. A circular buffer efficiently implements a fixed‑size queue without moving elements.

#define CAP 5
int q[CAP];
int front = 0, rear = 0, size = 0;

int enqueue(int x) {
    if (size == CAP) return -1; // full
    q[rear] = x;
    rear = (rear + 1) % CAP;
    ++size;
    return 0;
}

int dequeue(int *out) {
    if (size == 0) return -1; // empty
    *out = q[front];
    front = (front + 1) % CAP;
    --size;
    return 0;
}
Make it stickPractice what you've learned1 quiz · 3 practical tests · Optional activities from C Programming FundamentalsBrowse activities
Previous lessonStacks (LIFO) Using ArraysContinue to lesson 33Binary Trees – Basic OperationsBack to C Programming Fundamentals curriculum