1

I'm a begginer in C and had been trying to write a Stack program that can pull/push and display the elements in it. I had been running into some problems that I cannot pinpoint where it roots from. Here's my code so far:

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

#define STACK_SIZE 100

char ch;
int contents[STACK_SIZE], top = 0;

void display();

int stack_overflow(void)
{
        printf("Expression is too complex\n");
        exit(EXIT_FAILURE);
}

int stack_underflow(void)
{
        printf("Not enough operands in expression\n");
        exit(EXIT_FAILURE);
}

void make_empty(void)
{
    top = 0;                //makes the element at the top of the stack = 0
}

bool is_empty(void)
{
    return top == 0;        //returns 1 if the top is equal to 0
}

bool is_full(void)
{
    return top == STACK_SIZE;   //returns 1 if the top is the maximun stack size (already full)
}

void push(char i)
{
    if (is_full())
        stack_overflow();
    else
        contents[top++] = i;
}

char pop(void)
{
    if (is_empty())
        stack_underflow();
    else
        return contents[--top];
}


int main(void)
{
    int choice;
    int option = 1;
    int num;



    printf ("STACK OPERATION\n");
    while (option)
    {
        printf ("------------------------------------------\n");
        printf (" 1 --> PUSH \n");
        printf (" 2 --> POP \n");
        printf (" 3 --> DISPLAY \n");
        printf (" 4 --> EXIT \n");
        printf ("------------------------------------------\n");

        printf ("Enter your choice\n");
        scanf ("%d", &choice);

        switch (choice)
        {
            case 1: printf("Enter number you want to push: ");
                    scanf("%d",&num);
                    push(num);
                    break;

            case 2: pop();
                    break;

            case 3: display();
                    break;

            case 4: return;
        }

        fflush (stdin);
        printf ("Do you want to continue(Type 0 or 1)?\n");
        scanf ("%d", &option);
    }
}



void display()
{
    int i;

    printf("\n\n");

    for(i=0;i< top ;i++)
        printf("%d\n",contents[i]);
}


STACK OPERATION
------------------------------------------
 1 --> PUSH
 2 --> POP
 3 --> DISPLAY
 4 --> EXIT
------------------------------------------
Enter your choice
1
Enter number you want to push: 100
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
 1 --> PUSH
 2 --> POP
 3 --> DISPLAY
 4 --> EXIT
------------------------------------------
Enter your choice
1
Enter number you want to push: 500
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
 1 --> PUSH
 2 --> POP
 3 --> DISPLAY
 4 --> EXIT
------------------------------------------
Enter your choice
3


100
-12
Do you want to continue(Type 0 or 1)?
0

Process returned 0 (0x0) execution time : 11.474 s Press any key to continue.

Seems like it's not storing the elements correctly or maybe It's printing them wrong. Maybe using the top variable to ran through the loops is wrong? Any help would be much appreciated.

3
  • What is your actual output/error? Nothing jumps out as being wrong. Commented Sep 6, 2016 at 0:44
  • Your compiler was obligated to issue a diagnostic: C11 draft standard n1570: 6.8.6.4 The return statement 1 A return statement with an expression shall not appear in a function whose return type is void. A return statement without an expression shall only appear in a function whose return type is void. Note that this is a constraint. Commented Sep 6, 2016 at 0:47
  • When I printed the list the output should had given me '100' and '500' which are what I pushed into the stack. Instead I always get those negative values in between. Commented Sep 6, 2016 at 0:52

1 Answer 1

2

Your push function only gets a char:

void push(char i)

chars are limited from 0 to 255 (unsigned char) or -127 to 128 (signed char = char - that is the default).

The '500' you put in will thus be reduced (mod 256) to -12 - and that's what you get printed.

You need to make the parameter an int, and it should work.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, this was the reason. It's working now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.