Skip to content

Instantly share code, notes, and snippets.

@eduardopoleo
Created November 29, 2022 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduardopoleo/1d5240aae4078a3744c049bdb33cdd74 to your computer and use it in GitHub Desktop.
Save eduardopoleo/1d5240aae4078a3744c049bdb33cdd74 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#define MAX_DEPTH 100
int top = -1;
int stack[MAX_DEPTH];
char pop();
char pop() {
int value = stack[top];
top -= 1;
printf("inside pop! top %d, value: %d\n", top, value);
return value;
}
void push(int c);
void push(int c) {
stack[++top] = c;
printf("inside pushed! top %d, value: %d\n", top, stack[top]);
}
int value2;
int main() {
push(2);
push(400);
value2 = pop();
printf("pop! %d\n", value2);
value2 = pop();
printf("pop! %d\n", value2);
return 0;
}
/*
Print
inside pushed! top 0, value: 2
inside pushed! top 1, value: 400
inside pop! top 0, value: 400
pop! -112 <<--- WTF?
inside pop! top -1, value: 2
pop! 2
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment