CKTIS / C
finite state machines — concept C23
A finite-state machine models allowed states with an `enum` and events as validated explicit transitions; every state-event pair should define a next state or an error. An unhandled enumeration value, accidental `switch` fall-through, or changing state before a side effect succeeds creates invalid transitions.
Version: C23
Syntax
finite state machinesA finite-state machine models allowed states with an enum and events as validated explicit transitions; every state-event pair should define a next state or an error. An unhandled enumeration value, accidental switch fall-through, or changing state before a side effect succeeds creates invalid transitions.
In a protocol parser, store the current enum State and route every event through one transition function. It should first verify that the event is legal, perform the required effect, and change state only after that effect succeeds.
Handle an unknown state read from corrupted data, a repeated event, and a failed side effect. A default arm can return a defensive error, while compiler diagnostics should still reveal a missing case so a new enum value is not silently ignored.
Examples
#include <stdio.h>
enum State { IDLE, RUNNING, DONE };
int main(void) {
enum State state = IDLE;
if (state == IDLE) state = RUNNING;
if (state == RUNNING) state = DONE;
printf("%d\n", state == DONE);
return 0;
}
Result
1
A finite-state machine models allowed states with an `enum` and events as validated explicit transitions; every state-event pair should define a next state or an error. An unhandled enumeration value, accidental `switch` fall-through, or changing state before a side effect succeeds creates invalid transitions.