Select Git revision
Forked from
Peter Gerwinski / hp
46 commits behind the upstream repository.
Peter Gerwinski authored
fifo-8.c 819 B
#include <stdio.h>
#define FIFO_SIZE 10
int fifo[FIFO_SIZE];
int fifo_read = 0;
int fifo_write = 0;
void push (int x)
{
if ((fifo_write + 1) % FIFO_SIZE == fifo_read)
fprintf (stderr, "push(): FIFO is full\n");
else
{
fifo[fifo_write++] = x;
if (fifo_write >= FIFO_SIZE)
fifo_write = 0;
}
}
int pop (void)
{
if (fifo_read == fifo_write)
{
fprintf (stderr, "pop(): FIFO is empty\n");
return -1;
}
else
{
int result = fifo[fifo_read++];
if (fifo_read >= FIFO_SIZE)
fifo_read = 0;
return result;
}
}
int main (void)
{
push (3);
push (7);
push (137);
printf ("%d\n", pop ());
printf ("%d\n", pop ());
printf ("%d\n", pop ());
printf ("%d\n", pop ());
for (int i = 0; i < 15; i++)
push (i);
return 0;
}