C Stack Operations

Intermediate
6 min read
Updated: Nov 2026
1 example

The Core Operations

A tidy stack wraps its array and top index in a struct and exposes small functions: push, pop, peek, is_empty, is_full, and size. Operations that can fail return a status so callers can react. All core operations run in O(1) time.

Remember
struct Stack { int data[4]; int top; };
int  push(struct Stack *s, int v);
int  pop(struct Stack *s, int *out);
int  peek(const struct Stack *s, int *out);
int  is_empty(const struct Stack *s);

Cheat Sheet

Keep these facts handy while you work with Stack Operations in C.

TopicDetail
pushO(1)
popO(1)
peekO(1)
sizetop + 1

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Stack Array Implementation in C.

Example 1 — A stack struct with status codes

Each function reports success.

C
#include <stdio.h>

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

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

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

    push(&s, 1); push(&s, 2); push(&s, 3);
    printf("size=%d top=%d\n", s.top + 1, s.data[s.top]);
    pop(&s, &v); printf("popped %d\n", v);
    pop(&s, &v); printf("popped %d\n", v);
    printf("empty? %d\n", is_empty(&s));
    return 0;
}

How It Works

  1. The struct keeps the data and the top index together.
  2. pop returns the value through an output pointer so the return value can be a status.
  3. One item (1) remains, so the stack is not empty.

Edge Cases & Pitfalls

Check these before relying on Stack Operations in real programs.

status

Ignoring return codes

If you ignore the status, you may use an uninitialized v.

init

Starting top at 0

With top as an index, start at -1.

Key Takeaways

  • All O(1).
  • Return status codes.
  • Wrap state in a struct.

One line: A tidy stack wraps its array and top index in a struct and exposes small functions: push, pop, peek, is_empty, is_full, and size.

Frequently Asked Questions

Push (add), pop (remove), peek (read the top), is-empty, and size.
O(1) for push, pop, and peek in both array and linked implementations.
Continue with "Stack Array Implementation in C" next. Build a fixed-capacity stack on an array with a capacity macro. The C Stacks hub lists every topic in order.

Next: Stack Array Implementation in C

Build a fixed-capacity stack on an array with a capacity macro.

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.

9 people found this page helpful