C Queue Basics

Intermediate
6 min read
Updated: Nov 2026
1 example

First In, First Out

A queue behaves like a line at a counter: the first person to arrive is served first. Elements are added at the back and removed from the front. Queues keep order of arrival, which makes them ideal for scheduling, buffering, and level-by-level traversal.

Remember
enqueue(x);  /* add at back     */
dequeue();   /* remove from front */
front();     /* read front       */

Cheat Sheet

Keep these facts handy while you work with Queue Basics in C.

TopicDetail
FIFOFirst in, first out
EnqueueAdd at the back
DequeueRemove from the front
FrontNext item to be served

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Queue Operations.

Example 1 — Serve customers in order

Numbers are served in the order they joined.

C
#include <stdio.h>

int main(void) {
    int q[5], head = 0, tail = 0;

    for (int ticket = 101; ticket <= 103; ticket++) q[tail++] = ticket;

    printf("serving:");
    while (head < tail) printf(" %d", q[head++]);
    printf("\n");
    return 0;
}

How It Works

  1. Tickets are enqueued 101, 102, 103.
  2. Dequeue always reads at head.
  3. The output order matches the arrival order.

Edge Cases & Pitfalls

Check these before relying on Queue Basics in real programs.

stack

Mixing with a stack

Popping from the tail makes a stack, not a queue.

over

Fixed capacity

Check tail against the array size before enqueueing.

Key Takeaways

  • Oldest first.
  • Two ends.
  • Preserves arrival order.

One line: A queue behaves like a line at a counter: the first person to arrive is served first.

Frequently Asked Questions

First In, First Out: the earliest added element is the first removed.
Task scheduling, print spooling, buffering, breadth-first search, and message passing.
Continue with "C Queue Operations" next. Implement enqueue, dequeue, front, size, and is-empty. The C Queues hub lists every topic in order.

Next: Queue Operations

Implement enqueue, dequeue, front, size, and is-empty.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful