I’d recommend taking this approach — create a little language that gives you a clean way to describe the state machine, and then generate code based on that machine description.
https://stackoverflow.com/questions/3507683/implementing-hierarchical-state-machines-in-c

