Skip to main content

Stack Implementation Using Functions in C

3 min read Updated June 30, 2026
Share:
On this page (6sections)

About this program

This is an example program in stack programs. Read the concept first: C Array, then study the code and output below.

Definition

A stack is a basic computer science data structure and can be defined in an abstract, implementation-free manner, or it can be generally defined as a linear list of items in which all additions and deletion are restricted to one end that is Top.

Simple Stack Program using functions in C Programming

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#define MAX_SIZE 5

int item, choice, i;
int arr_stack[MAX_SIZE];
int top = 0;
int exit_p = 1;

void push() {
    if (top == MAX_SIZE)
        printf("\n## Stack is Full!");
    else {
        printf("\nEnter The Value to be pushed : ");
        scanf("%d", &item);
        printf("\n## Position : %d , Pushed Value  : %d ", top, item);
        arr_stack[top++] = item;
    }
}

void pop() {
    if (top == 0)
        printf("\n## Stack is Empty!");
    else {
        --top;
        printf("\n## Position : %d , Popped Value  : %d ", top, arr_stack[top]);
    }
}

void display() {
    printf("\n## Stack Size : %d ", top);
    for (i = (top - 1); i >= 0; i--)
        printf("\n## Position : %d , Value  : %d ", i, arr_stack[i]);
}

int main() {
    printf("\nSimple Stack Example - Array and Functions");
    do {
        printf("\n\nStack Main Menu");

        printf("\n1.Push \n2.Pop \n3.Display \nOthers to exit");
        printf("\nEnter Your Choice : ");
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                push();
                break;
            case 2:
                pop();
                break;
            case 3:
                display();
                break;
            default:
                exit_p = 0;
                break;
        }
    } while (exit_p);

    return 0;
}

Sample Output:

Simple Stack Example - Array and Functions

Stack Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 1

Enter The Value to be pushed : 1

## Position : 0 , Pushed Value  : 1

Stack Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 1

Enter The Value to be pushed : 2

## Position : 1 , Pushed Value  : 2

Stack Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 1

Enter The Value to be pushed : 3

## Position : 2 , Pushed Value  : 3

Stack Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 1

Enter The Value to be pushed : 4

## Position : 3 , Pushed Value  : 4

Stack Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 1

Enter The Value to be pushed : 5

## Position : 4 , Pushed Value  : 5

Stack Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 1

## Stack is Full!

Stack Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 2

## Position : 4 , Popped Value  : 5

Stack Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 2

## Position : 3 , Popped Value  : 4

Stack Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 2

## Position : 2 , Popped Value  : 3

Stack Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 2

## Position : 1 , Popped Value  : 2

Stack Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 2

## Position : 0 , Popped Value  : 1

Stack Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 2

## Stack is Empty!

Stack Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 4

------------------
(program exited with code: 0)

Learn the concept first, then study the code:

Frequently Asked Questions

What does this C program do?
It is a C example program that demonstrates Stack Implementation Using Functions, including the complete source code and the expected sample output.
How do I compile and run this C program?
Save the code in a `.c` file, compile it with `gcc filename.c -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses loops to iterate over data, arrays and conditional logic, illustrating a common pattern in C programming.

Related Tutorials

Search tutorials