Consider this embedded C code using a state transition table. What will be printed when input = 1?
typedef enum {STATE_IDLE, STATE_RUN, STATE_STOP} State;
typedef struct {
State current_state;
void (*action)(void);
} Transition;
void action_idle() { printf("Idle\n"); }
void action_run() { printf("Run\n"); }
void action_stop() { printf("Stop\n"); }
Transition state_table[3][2] = {
{{STATE_IDLE, action_idle}, {STATE_RUN, action_run}},
{{STATE_RUN, action_run}, {STATE_STOP, action_stop}},
{{STATE_STOP, action_stop}, {STATE_IDLE, action_idle}}
};
int main() {
State current = STATE_IDLE;
int input = 1;
Transition t = state_table[current][input];
t.action();
return 0;
}Look at the state_table row for STATE_IDLE and column for input = 1.
The state transition table entry for STATE_IDLE and input 1 points to STATE_RUN with action_run. So, Run is printed.
In a state transition table for a device with 4 states and 3 inputs, how many entries should the table have?
Think about rows as states and columns as inputs.
The table has one row per state and one column per input. So total entries = 4 states * 3 inputs = 12.
What error will this code produce?
typedef enum {S0, S1} State;
void action0() { printf("A0\n"); }
void action1() { printf("A1\n"); }
typedef struct {
State next_state;
void (*action)();
} Transition;
Transition table[2][2] = {
{ {S1, action0}, {S0, action1} },
{ {S0, action1}, {S1, action0} }
};
int main() {
table[0][1].action();
return 0;
}Check the initialization of the 2D array table.
The second row of table has only one initializer instead of two, causing a compilation error due to incomplete initialization.
Which option correctly declares a function pointer named action in a struct for a function returning void and taking no parameters?
Remember the syntax for a pointer to a function with no parameters returning void.
Option D correctly declares action as a pointer to a function taking void parameters and returning void. Option D is also valid in C but less explicit; however, for embedded C best practice, (void) is preferred to indicate no parameters.
Given the state transition table and initial state STATE_A, what is the final state after inputs 0, 1, 1, 0?
typedef enum {STATE_A, STATE_B, STATE_C} State;
typedef struct {
State next_state;
void (*action)(void);
} Transition;
void actA() { printf("A\n"); }
void actB() { printf("B\n"); }
void actC() { printf("C\n"); }
Transition table[3][2] = {
{{STATE_B, actB}, {STATE_C, actC}},
{{STATE_A, actA}, {STATE_C, actC}},
{{STATE_C, actC}, {STATE_A, actA}}
};
int main() {
State current = STATE_A;
int inputs[] = {0, 1, 1, 0};
int len = 4;
for (int i = 0; i < len; i++) {
Transition t = table[current][inputs[i]];
t.action();
current = t.next_state;
}
printf("Final state: %d\n", current);
return 0;
}Trace each input step by step using the table.
Starting at STATE_A:
Input 0: next STATE_B
Input 1: from STATE_B input 1 → STATE_C
Input 1: from STATE_C input 1 → STATE_A
Input 0: from STATE_A input 0 → STATE_B
But careful: The code prints final state as integer, so final state is STATE_B (1). However, the code prints final state as %d, so the final state integer is 1 (STATE_B). The correct answer is STATE_B.