Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to implement a stack using an array with push, pop, and display in C

1 Answer

0 votes
#include <stdio.h>

#define SIZE 128

int stack[SIZE], top = -1;

void push(int value) {
    if (top == SIZE - 1)
        printf("Stack Overflow\n");
    else
        stack[++top] = value;
}

void pop() {
    if (top == -1)
        printf("Stack Underflow\n");
    else
        top--;
}

void display() {
    if (top == -1)
        printf("Stack is empty\n");
    else {
        printf("Stack:\n");
        for (int i = top; i >= 0; i--)
            printf("%d\n", stack[i]);
    }
}

int main() {
    push(10);
    push(20);
    push(30);
    push(40);
    
    display();
    
    pop();
    
    puts("");
    
    display();
    
    return 0;
}


  
 
/* 
run:
 
Stack:
40
30
20
10

Stack:
30
20
10
 
*/

 



answered Nov 4, 2025 by avibootz

Related questions

1 answer 128 views
1 answer 261 views
2 answers 160 views
1 answer 111 views
111 views asked Jun 6, 2024 by avibootz
1 answer 136 views
1 answer 147 views
...