Skip to content
Snippets Groups Projects
Select Git revision
  • d48edcd327479e7a230e1a34684f251a5cd47337
  • master default protected
  • 2018ws
  • 2017ws
  • 2016ws
5 results

fifo-8.c

Blame
  • Forked from Peter Gerwinski / hp
    46 commits behind the upstream repository.
    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;
    }