A stack is an abstract data type that stores elements in a Last-In, First-Out (LIFO) order. An array can be used to implement a stack when the maximum number of elements is known in advance. In the array implementation a single one-dimensional array holds the stack elements and an integer variable top keeps track of the index of the current top element. Conventionally, top is initialised to -1 to indicate an empty stack. When an element is pushed, top is incremented and the element is stored at array index top. When an element is popped, the element at index top is removed and top is decremented.
Before implementing the operations, follow these steps to define and create an empty stack in C:
push() inserts an element at the top of the stack. The new element is always stored at index top after incrementing top.
pop() removes and returns the element at the top of the stack. The element is always removed from the current top position.
The stack elements are displayed starting from the top down to the bottom (index 0).
It is often useful to have helper functions:
Note: The example in some older tutorials uses <conio.h> and clrscr(). <conio.h> is non-standard and compiler-specific; the code below uses standard headers <stdio.h> and <stdlib.h> for portability.
#include <stdio.h> #include <stdlib.h> #define SIZE 10 int stack[SIZE]; int top = -1; /* Function prototypes */ void push(int value); int pop(void); int peek(void); int isEmpty(void); int isFull(void); void display(void); int main(void) { int choice, value, deleted; while (1) { printf("
* MENU *
"); printf("1. Push
2. Pop
3. Peek (Top element)
4. Display
5. Exit
"); printf("Enter your choice: "); if (scanf("%d", &choice) != 1) { /* invalid input: clear input and continue */ int ch; while ((ch = getchar()) != EOF && ch != '
'); printf("Invalid input. Try again.
"); continue; } switch (choice) { case 1: printf("Enter the value to insert: "); if (scanf("%d", &value) == 1) { push(value); } else { int ch; while ((ch = getchar()) != EOF && ch != '
'); printf("Invalid value.
"); } break; case 2: deleted = pop(); if (deleted != INT_MIN) { printf("Deleted: %d
", deleted); } break; case 3: if (!isEmpty()) { printf("Top element: %d
", peek()); } else { printf("Stack is EMPTY
"); } break; case 4: display(); break; case 5: exit(0); default: printf("Wrong selection! Try again!
"); } } return 0; } void push(int value) { if (isFull()) { printf("Stack is FULL!!! Insertion is not possible!!!
"); return; } top++; stack[top] = value; printf("Insertion success: %d
", value); } int pop(void) { if (isEmpty()) { printf("Stack is EMPTY!!! Deletion is not possible!!!
"); return INT_MIN; /* sentinel for error */ } int value = stack[top]; top--; return value; } int peek(void) { if (isEmpty()) { return INT_MIN; } return stack[top]; } int isEmpty(void) { return (top == -1); } int isFull(void) { return (top == SIZE - 1); } void display(void) { if (isEmpty()) { printf("Stack is EMPTY!!!
"); return; } printf("Stack elements (top to bottom):
"); for (int i = top; i >= 0; i--) { printf("%d
", stack[i]); } }
Suppose we perform the operations: push(10), push(20), push(30), pop(), display(). The stack evolves as follows:

![]() | Explore Courses for Computer Science Engineering (CSE) exam |
![]() | Get EduRev Notes directly in your Google search |