Stack implementation in C uses an array with a top pointer that tracks the current topmost element. A stack is a LIFO (Last In, First Out) data structure — the last element pushed is the first one popped. It supports three core operations: PUSH (insert), POP (remove), and PEEK (view without removing).
Stacks are used everywhere: function call management, expression evaluation, undo operations, DFS graph traversal, and compiler parsing.
Stack Operations
- PUSH: Add an element to the top. Fails if the stack is full (overflow).
- POP: Remove and return the top element. Fails if empty (underflow).
- PEEK: Return the top element without removing it.
- isEmpty: True if
top == -1. - isFull: True if
top == SIZE - 1.
Stack Implementation in C — Array Based
#include <stdio.h>
#define SIZE 10
int stack[SIZE];
int top = -1;
int isFull(void) { return top == SIZE - 1; }
int isEmpty(void) { return top == -1; }
void push(int val)
{
if (isFull()) {
printf("Stack overflow — cannot push %d.\n", val);
return;
}
stack[++top] = val;
printf("Pushed: %d\n", val);
}
int pop(void)
{
if (isEmpty()) {
printf("Stack underflow — nothing to pop.\n");
return -1;
}
return stack[top--];
}
int peek(void)
{
if (isEmpty()) {
printf("Stack is empty.\n");
return -1;
}
return stack[top];
}
void display(void)
{
int i;
if (isEmpty()) {
printf("Stack is empty.\n");
return;
}
printf("Stack (top → bottom): ");
for (i = top; i >= 0; i--)
printf("%d ", stack[i]);
printf("\n");
}
int main(void)
{
push(10);
push(20);
push(30);
display();
printf("Peek: %d\n", peek());
printf("Popped: %d\n", pop());
printf("Popped: %d\n", pop());
display();
return 0;
}
How to Compile and Run
gcc -Wall -o stack stack.c
./stack
Sample Output
Pushed: 10 Pushed: 20 Pushed: 30 Stack (top → bottom): 30 20 10 Peek: 30 Popped: 30 Popped: 20 Stack (top → bottom): 10
Interactive Menu Version
A menu-driven version so you can test all operations interactively:
#include <stdio.h>
#define SIZE 10
int stack[SIZE];
int top = -1;
int isFull(void) { return top == SIZE - 1; }
int isEmpty(void) { return top == -1; }
void push(int val)
{
if (isFull()) { printf("Overflow.\n"); return; }
stack[++top] = val;
}
int pop(void)
{
if (isEmpty()) { printf("Underflow.\n"); return -1; }
return stack[top--];
}
void display(void)
{
int i;
if (isEmpty()) { printf("Empty stack.\n"); return; }
printf("Stack: ");
for (i = top; i >= 0; i--) printf("%d ", stack[i]);
printf(" <- top\n");
}
int main(void)
{
int choice, val;
do {
printf("\n1-Push 2-Pop 3-Peek 4-Display 5-Exit\nOption: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Value: ");
scanf("%d", &val);
if (!isFull()) push(val);
else printf("Overflow.\n");
break;
case 2:
val = pop();
if (val != -1) printf("Popped: %d\n", val);
break;
case 3:
val = isEmpty() ? -1 : stack[top];
if (val != -1) printf("Top: %d\n", val);
break;
case 4:
display();
break;
}
} while (choice != 5);
return 0;
}
How the Implementation Works
top = -1: The conventional sentinel for an empty stack. The first push setstopto 0.- PUSH uses
++top(pre-increment): Increments first, then assigns. So the first push places the value at index 0. - POP uses
top--(post-decrement): Reads fromstack[top], then decrements. The element is not erased from memory — it’s just unreachable until another push overwrites it. - Overflow check:
top == SIZE - 1means all slots are filled. Push one more and you’d write past the array boundary — undefined behavior.
Time and Space Complexity
| Operation | Time | Space |
|---|---|---|
| PUSH | O(1) | O(1) |
| POP | O(1) | O(1) |
| PEEK | O(1) | O(1) |
| Total (n elements) | — | O(n) |
What This Program Teaches
- The LIFO principle in practice — why function calls and undo stacks use this structure
- Pre-increment vs post-decrement:
stack[++top]for push,stack[top--]for pop — the order matters - Overflow and underflow guards — missing these causes silent array out-of-bounds writes
- How a fixed-size array becomes a dynamic-feeling structure with just an integer index
Related Programs
- Stack Implementation in C Using Linked Lists
- Infix to Postfix Conversion in C (uses a stack)
- BFS Algorithm in C
- C Aptitude Questions and Answers
As an Amazon Associate we earn from qualifying purchases.
Recommended Book
Stacks, arrays, and pointer-based data structures are covered in chapters 5 and 6 of The C Programming Language by Kernighan & Ritchie. Also on Amazon.com.