[ create a new paste ] login | about

Link: http://codepad.org/nMldZcQh    [ raw code | output | fork | 1 comment ]

joshua_cheek - C, pasted on Nov 15:
#include<stdio.h>

#define FSM
#define STATE(x)      s_##x :
#define NEXTSTATE(x)  goto s_##x

int main()
{

  int x = 5;
  
  FSM 
  {

    STATE(x) 
    {
      printf( "in state x\n" );
      NEXTSTATE(y);
    }

    STATE(y) 
    {
      --x;
      printf( "in state y\n" );
      if (x == 0) 
        NEXTSTATE(x);
      else if( x < 0 )
        NEXTSTATE(end);
      else 
        NEXTSTATE(y);  
    }
  
    STATE(end);
  }
  
  getchar();  
  return 0;
}


Output:
1
2
3
4
5
6
7
8
in state x
in state y
in state y
in state y
in state y
in state y
in state x
in state y


Create a new paste based on this one


Comments:
posted by joshua_cheek on Nov 15
Implementing a Finite State Machine in C.
reply