C Stack Overflow and Underflow

Intermediate
6 min read
Updated: Nov 2026
1 example

When a Stack Goes Out of Bounds

Overflow happens when you push onto a full stack; underflow when you pop an empty one. Both indicate a bug or bad input and must be detected, not ignored. Separately, a call-stack overflow occurs when recursion goes too deep, typically from a missing base case; the operating system aborts the program.

Remember
if (top == CAP - 1) return OVERFLOW;
if (top == -1)      return UNDERFLOW;

Cheat Sheet

Keep these facts handy while you work with Stack Overflow and Underflow in C.

TopicDetail
OverflowPush on full
UnderflowPop on empty
HandlingReturn a status or error code
Call stackMissing base case in recursion

C Examples

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

Example 1 — Report both errors

A stack of capacity 2.

C
#include <stdio.h>

struct Stack { int data[2]; int top; };

static const char *push(struct Stack *s, int v) {
    if (s->top == 1) return "overflow";
    s->data[++s->top] = v;
    return "ok";
}
static const char *pop(struct Stack *s, int *out) {
    if (s->top < 0) return "underflow";
    *out = s->data[s->top--];
    return "ok";
}

int main(void) {
    struct Stack s = {{0}, -1};
    int v = 0;

    printf("push 1: %s\n", push(&s, 1));
    printf("push 2: %s\n", push(&s, 2));
    printf("push 3: %s\n", push(&s, 3));
    for (int i = 0; i < 3; i++) {
        const char *r = pop(&s, &v);
        if (r[0] == 'o') printf("pop: %d\n", v);
        else printf("pop: %s\n", r);
    }
    return 0;
}

How It Works

  1. The third push is rejected with "overflow".
  2. Two pops succeed; the third finds the stack empty.
  3. Returning a message string keeps the demo readable; real code usually returns an enum or int.

Edge Cases & Pitfalls

Check these before relying on Stack Overflow and Underflow in real programs.

silent

Silent failure

Ignoring the status leaves the program using stale data.

abort

Using assert in production

assert disappears with NDEBUG. Use real checks for runtime conditions.

Key Takeaways

  • Overflow: full push.
  • Underflow: empty pop.
  • Always check and report.

One line: Overflow happens when you push onto a full stack; underflow when you pop an empty one.

Frequently Asked Questions

Pushing onto a full stack, or exhausting the call stack through very deep or infinite recursion.
Trying to pop or peek from an empty stack.
Continue with "C Expression Conversion" next. Convert infix expressions to postfix with an operator stack. The C Stacks hub lists every topic in order.

Next: Expression Conversion

Convert infix expressions to postfix with an operator stack.

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.

12 people found this page helpful