Stack Using Array in C (Menu-Driven Program)
In this lesson: Menu-driven stack program in C using array — push, pop, peek, display — with complete code and sample output.
The goal: a working stack program
This is a hands-on practice program. If you want to first understand what a stack is and how LIFO works, read the stack in C concept lesson. Here we go straight to building a complete, menu-driven stack program using an array that you can compile and run.
What the program will do
The program keeps showing a menu and performs whatever the user picks:
- 1. Push — add a number to the top
- 2. Pop — remove the top number
- 3. Peek — see the top number without removing it
- 4. Display — show all numbers from top to bottom
- 5. Exit
The complete program
#include <stdio.h>
#define SIZE 100
int stack[SIZE];
int top = -1;
void push(int x) {
if (top == SIZE - 1) { printf("Stack full\n"); return; }
stack[++top] = x;
printf("%d pushed\n", x);
}
void pop() {
if (top == -1) { printf("Stack empty\n"); return; }
printf("%d popped\n", stack[top--]);
}
void peek() {
if (top == -1) { printf("Stack empty\n"); return; }
printf("Top = %d\n", stack[top]);
}
void display() {
if (top == -1) { printf("Stack empty\n"); return; }
printf("Stack: ");
for (int i = top; i >= 0; i--) printf("%d ", stack[i]);
printf("\n");
}
int main() {
int choice, value;
while (1) {
printf("\n1.Push 2.Pop 3.Peek 4.Display 5.Exit: ");
scanf("%d", &choice);
switch (choice) {
case 1: printf("Value: "); scanf("%d", &value); push(value); break;
case 2: pop(); break;
case 3: peek(); break;
case 4: display(); break;
case 5: return 0;
default: printf("Invalid\n");
}
}
}A sample run
1.Push 2.Pop 3.Peek 4.Display 5.Exit: 1
Value: 10
10 pushed
1.Push 2.Pop 3.Peek 4.Display 5.Exit: 1
Value: 20
20 pushed
1.Push 2.Pop 3.Peek 4.Display 5.Exit: 4
Stack: 20 10
1.Push 2.Pop 3.Peek 4.Display 5.Exit: 2
20 popped
1.Push 2.Pop 3.Peek 4.Display 5.Exit: 5
Dry run of push and pop
| Action | top | Stack contents |
|---|---|---|
| start | -1 | (empty) |
| push 10 | 0 | 10 |
| push 20 | 1 | 10, 20 |
| pop → 20 | 0 | 10 |
Each push moves top up before storing; each pop reads at top then moves it down. The display loop runs from top down to 0.
Common mistakes
- Starting
topat 0 instead of -1 for an empty stack. - Forgetting the full/empty checks, causing overflow or reading invalid memory.
- Displaying from 0 upward instead of top downward (wrong order).
- Mixing
++top(pre) andtop++(post) incorrectly in push/pop.
Add a "count" option that prints how many items are in the stack (that is just top + 1). Then try converting the fixed array into a dynamically allocated one.
Summary
- An array stack uses an array plus a
topindex starting at-1. - A menu-driven loop exercises push, pop, peek and display.
- Push moves
topup then stores; pop reads then moves down. - Display runs from
topdown to 0 to show LIFO order. - Always guard against full and empty conditions.
Frequently Asked Questions
How do you implement a stack using an array in C?
top to mark the current top position, starting at -1. push increases top and stores the value; pop returns the value at top and decreases it. You check for full before pushing and empty before popping.What is a menu-driven stack program?
What is the peek operation in a stack?
peek (sometimes called top) returns the value of the item currently on top of the stack without removing it. It lets you look at the next item to be popped while leaving the stack unchanged.How do you display all elements of an array stack?
top index down to 0, printing each element. Going from top down shows the items in the order they would be popped, which reflects the LIFO nature of the stack.