top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

Design a state machine in C for the following problem by using macros?

0 votes

Can anyone help me with a C program to design a simple state machine by using macros?

S1,S2,S3,S4 are the states and E1, E2,E3,E4,E5,E6,E7,E8 are events ...

when states moving from one state to another respective event should trigger
s1 to s2 ------> E1
s2 to s3 ------> E2
s3 to s4 ------> E3
s4 to s1 ------> E4
s1 to s4 ------> E5
s4 to s3 ------> E6
s3 to s2 ------> E7
s2 to s1 ------> E8

Please share the C program

posted Mar 15, 2016 by Suchakravarthi Sripathi

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
Best answer

Let me try in whatever way I could understand.

enum state {S1, S2, S3, S4};
enum event {E1, E2, E3, E4, E5, E6, E7, E8};

Now lets assume funcE1 is the function for the event E1, funcE2 is for the E2 and so on. And also assume a func for the error condition Like S1-->S3 which does not exist as funcErr

So S1 State Machine

StateS1[State] = {
    funcE5 }

S2 State Machine

StateS2[State] = {
    funcErr }

And rest is so on. Remember all are function pointer so say you are in state S1 and move to say state Sx then just call StateS1" target="_blank">Sx

This is the design not the C code so you need to do the remaining talk of coding it. Comment if have any question -

answer Mar 16, 2016 by Salil Agrawal
Similar Questions
+2 votes

Please share a sample program with detail code.

0 votes

Does someone have a ready-made code then please share? it would be very helpful?

+1 vote

I am a newbie and using fedora machine, please help with point by point C programming setup.

+3 votes

int main()
    char ptr[]= {'a','b','c','0','e'};
    char str[]= "abc0e";
    printf("\nptr = %s\t len = %d\n",ptr, strlen(ptr));
    printf("\nstr = %s\t len = %d\n",str, strlen(str));
    return 0;

Output : ptr = abc0e len = 6
str = abc0e len = 5

Why the length for ptr is 6 ? Can someone please explain it ?

Contact Us
+91 9880187415
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Karnataka INDIA.