Bird
Raised Fist0
Embedded Cprogramming~20 mins

State transition table approach in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
State Machine Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple state machine

Consider this embedded C code using a state transition table. What will be printed when input = 1?

Embedded C
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;
}
AIdle
BRun
CStop
DNo output
Attempts:
2 left
💡 Hint

Look at the state_table row for STATE_IDLE and column for input = 1.

🧠 Conceptual
intermediate
1:30remaining
Understanding state transition table indexing

In a state transition table for a device with 4 states and 3 inputs, how many entries should the table have?

A12
B7
C4
D3
Attempts:
2 left
💡 Hint

Think about rows as states and columns as inputs.

🔧 Debug
advanced
2:30remaining
Identify the error in this state transition table code

What error will this code produce?

Embedded C
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;
}
APrints 'A1' without error
BRuntime segmentation fault
CCompilation error due to missing initializer in table
DPrints 'A0' without error
Attempts:
2 left
💡 Hint

Check the initialization of the 2D array table.

📝 Syntax
advanced
1:30remaining
Correct syntax for function pointer in state transition struct

Which option correctly declares a function pointer named action in a struct for a function returning void and taking no parameters?

Avoid (*action)();
Bvoid *action(void);
Cvoid action(void*);
Dvoid (*action)(void);
Attempts:
2 left
💡 Hint

Remember the syntax for a pointer to a function with no parameters returning void.

🚀 Application
expert
3:00remaining
Determine final state after sequence of inputs

Given the state transition table and initial state STATE_A, what is the final state after inputs 0, 1, 1, 0?

Embedded C
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;
}
ASTATE_B
BSTATE_C
CCompilation error
DSTATE_A
Attempts:
2 left
💡 Hint

Trace each input step by step using the table.

Practice

(1/5)
1.

What is the main purpose of a state transition table in embedded C programming?

easy
A. To store global variables for the program
B. To map current states and events to the next states
C. To handle memory allocation dynamically
D. To perform arithmetic calculations efficiently

Solution

  1. Step 1: Understand the role of state transition tables

    A state transition table is used to define how a system moves from one state to another based on events.
  2. Step 2: Identify the correct purpose

    Mapping current states and events to next states is exactly what a state transition table does.
  3. Final Answer:

    To map current states and events to the next states -> Option B
  4. Quick Check:

    State transition table = map states and events [OK]
Hint: Think: state + event leads to next state [OK]
Common Mistakes:
  • Confusing state tables with variable storage
  • Thinking it manages memory
  • Assuming it performs calculations
2.

Which of the following is the correct way to declare a state transition table in embedded C?

typedef enum {STATE_IDLE, STATE_RUN, STATE_STOP} State;
typedef enum {EVENT_START, EVENT_STOP} Event;

State state_table[][2] = { ... };
easy
A. State state_table[3][2];
B. int state_table[2][3];
C. State state_table[2][2];
D. Event state_table[3][2];

Solution

  1. Step 1: Analyze the enums and dimensions

    There are 3 states and 2 events, so the table should have 3 rows (states) and 2 columns (events).
  2. Step 2: Match the declaration with dimensions

    State state_table[3][2]; declares a State 2D array with 3 rows and 2 columns, matching the enums.
  3. Final Answer:

    State state_table[3][2]; -> Option A
  4. Quick Check:

    Rows = states, columns = events [OK]
Hint: Rows = states, columns = events in table declaration [OK]
Common Mistakes:
  • Swapping rows and columns
  • Using wrong data type for table
  • Mismatching array size with enums
3.

Given the following code snippet, what will be the output?

typedef enum {IDLE, RUN, STOP} State;
typedef enum {START, STOP_EVENT} Event;

State state_table[3][2] = {
  {RUN, IDLE},
  {RUN, STOP},
  {IDLE, IDLE}
};

State current_state = IDLE;
Event event = START;

current_state = state_table[current_state][event];
printf("%d", current_state);
medium
A. 2
B. 0
C. 1
D. Compilation error

Solution

  1. Step 1: Identify current state and event indices

    current_state = IDLE (0), event = START (0)
  2. Step 2: Find next state from table

    state_table[0][0] = RUN (1)
  3. Final Answer:

    1 -> Option C
  4. Quick Check:

    state_table[0][0] = 1 (RUN) [OK]
Hint: Use enum values as indexes to find next state [OK]
Common Mistakes:
  • Mixing event and state indices
  • Assuming output is enum name, not int
  • Forgetting zero-based indexing
4.

Identify the error in the following state transition table code snippet:

typedef enum {S0, S1} State;
typedef enum {E0, E1} Event;

State state_table[2][2] = {
  {S1, S0},
  {S0}
};
medium
A. Wrong enum type used for state_table
B. State enum values are not initialized
C. Array size should be [2][1] instead of [2][2]
D. Missing second element in second row of the table

Solution

  1. Step 1: Check array initialization

    The first row has 2 elements, but the second row has only 1 element.
  2. Step 2: Compare with declared size

    The array is declared as 2x2, so each row must have 2 elements.
  3. Final Answer:

    Missing second element in second row of the table -> Option D
  4. Quick Check:

    Array rows must match declared column size [OK]
Hint: Check all rows have correct number of elements [OK]
Common Mistakes:
  • Ignoring incomplete row initialization
  • Confusing enum initialization with array size
  • Assuming default values fill missing elements
5.

You want to implement a state machine with states OFF, ON, and ERROR, and events POWER_ON, POWER_OFF, and FAULT. Using a state transition table approach, which of the following correctly defines the next state when the current state is ON and the event is FAULT?

typedef enum {OFF, ON, ERROR} State;
typedef enum {POWER_ON, POWER_OFF, FAULT} Event;

State state_table[3][3] = {
  {ON, OFF, ERROR},
  {ON, OFF, ???},
  {OFF, OFF, ERROR}
};
hard
A. ERROR
B. ON
C. OFF
D. POWER_OFF

Solution

  1. Step 1: Identify current state and event indices

    Current state is ON (index 1), event is FAULT (index 2).
  2. Step 2: Determine correct next state for fault event

    On fault, the system should move to ERROR state.
  3. Step 3: Fill the missing table entry

    state_table[1][2] should be ERROR.
  4. Final Answer:

    ERROR -> Option A
  5. Quick Check:

    Fault event from ON leads to ERROR state [OK]
Hint: Fault event usually leads to ERROR state [OK]
Common Mistakes:
  • Using event names as states
  • Assigning ON or OFF instead of ERROR on fault
  • Confusing event and state enums
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard