Page 74 - Programming Microcontrollers in C
P. 74
Functions 59
putchar(c);
printf(“\n”);
return 0;
}
The following code is to be compiled in a separate file from the code
above:
/* the stack function */
#define MAX 100
static int buffer[MAX];
static int sp;
void push(int x)
{
if(sp < MAX)
buffer[sp++]=x;
else
{
printf(“stack overflow\n”);
exit(1);
}
}
/* the unstacking function */
int pull(void)
{
if(sp > 0)
return buffer[—sp];
else
{
printf(“stack underflow\n”);
exit(1);
}
}
A stack is a last in, first out (LIFO) structure. Therefore, a stack
can be used to reverse the order of data sent to it. The above program
uses a stack operation. The functions push and pull identified in the