Bird
Raised Fist0
Embedded Cprogramming~20 mins

Hierarchical state machine concept in Embedded C - Practice Problems & Coding Challenges

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

Consider a hierarchical state machine with states STATE_A and STATE_B, where STATE_B has substates SUBSTATE_B1 and SUBSTATE_B2. The machine starts in STATE_A. What is the output after transitioning to SUBSTATE_B2?

Embedded C
#include <stdio.h>

enum State {STATE_A, STATE_B};
enum SubState {SUBSTATE_B1, SUBSTATE_B2};

int main() {
    enum State current_state = STATE_A;
    enum SubState current_substate = SUBSTATE_B1;

    // Transition to STATE_B
    current_state = STATE_B;
    // Transition to SUBSTATE_B2
    current_substate = SUBSTATE_B2;

    if (current_state == STATE_B) {
        if (current_substate == SUBSTATE_B1) {
            printf("In SUBSTATE_B1\n");
        } else if (current_substate == SUBSTATE_B2) {
            printf("In SUBSTATE_B2\n");
        }
    } else {
        printf("In STATE_A\n");
    }
    return 0;
}
AIn SUBSTATE_B1
BIn STATE_A
CIn SUBSTATE_B2
DNo output
Attempts:
2 left
💡 Hint

Check which state and substate the machine is in before printing.

🧠 Conceptual
intermediate
1:30remaining
Understanding parent state behavior

In a hierarchical state machine, what happens when an event is not handled in a substate?

AThe event is ignored and no action is taken.
BThe event is passed up to the parent state to handle.
CThe machine resets to the initial state.
DThe machine crashes due to unhandled event.
Attempts:
2 left
💡 Hint

Think about how hierarchical states share behavior.

🔧 Debug
advanced
2:00remaining
Identify the bug in hierarchical state transition

What error does the following code produce when trying to transition from STATE_B to SUBSTATE_B2?

Embedded C
#include <stdio.h>

enum State {STATE_A, STATE_B};
enum SubState {SUBSTATE_B1, SUBSTATE_B2};

int main() {
    enum State current_state = STATE_A;
    enum SubState current_substate;

    current_state = STATE_B;
    // Forgot to initialize current_substate

    if (current_state == STATE_B) {
        if (current_substate == SUBSTATE_B2) {
            printf("In SUBSTATE_B2\n");
        } else {
            printf("In other substate\n");
        }
    }
    return 0;
}
AUndefined behavior due to uninitialized variable
BRuntime error: Segmentation fault
CNo output printed
DCompilation error: 'current_substate' used uninitialized
Attempts:
2 left
💡 Hint

Check if all variables are properly initialized before use.

📝 Syntax
advanced
1:30remaining
Syntax error in hierarchical state machine code

Which option contains the correct syntax to define a hierarchical state machine with a parent state STATE_B and substate SUBSTATE_B1?

Aenum State {STATE_A, STATE_B}; enum SubState {SUBSTATE_B1, SUBSTATE_B2};
Benum State {STATE_A, STATE_B {SUBSTATE_B1, SUBSTATE_B2}};
Cenum State {STATE_A, STATE_B}; enum State SUBSTATE_B1, SUBSTATE_B2;
Denum State {STATE_A, STATE_B}; enum SubState SUBSTATE_B1, SUBSTATE_B2;
Attempts:
2 left
💡 Hint

Remember how enums are declared separately in C.

🚀 Application
expert
2:30remaining
Number of active states in hierarchical state machine

In a hierarchical state machine with 3 parent states, each having 2 substates, how many states are active simultaneously during normal operation?

A3
B6
C1
D4
Attempts:
2 left
💡 Hint

Think about how hierarchical states are active together.

Practice

(1/5)
1. What is the main benefit of using a hierarchical state machine in embedded C programming?
easy
A. It removes the need for any state transitions.
B. It eliminates the need for events to change states.
C. It allows states to run in parallel without interaction.
D. It organizes states into groups, making complex behavior easier to manage.

Solution

  1. Step 1: Understand hierarchical state machine purpose

    Hierarchical state machines group related states inside bigger states to simplify complex logic.
  2. Step 2: Identify the benefit

    This grouping helps share common actions and manage transitions more clearly.
  3. Final Answer:

    It organizes states into groups, making complex behavior easier to manage. -> Option D
  4. Quick Check:

    Grouping states = easier management [OK]
Hint: Think of states like folders inside folders for clarity [OK]
Common Mistakes:
  • Thinking events are not needed in hierarchical state machines
  • Believing states run independently without interaction
  • Assuming no transitions are required
2. Which of the following is the correct way to define a sub-state inside a parent state in embedded C code for a hierarchical state machine?
easy
A. typedef enum { STATE_PARENT, STATE_CHILD } State;
B. typedef enum { STATE_PARENT, STATE_CHILD } State; // with STATE_CHILD as a separate enum
C. typedef enum { STATE_PARENT, STATE_CHILD } State; // with STATE_CHILD nested inside STATE_PARENT logically
D. typedef enum { STATE_PARENT, STATE_CHILD } State; // with STATE_CHILD declared inside STATE_PARENT block

Solution

  1. Step 1: Understand enum usage for states

    In embedded C, states are often defined as enums. Logical nesting is done by naming and design, not by code nesting.
  2. Step 2: Identify correct sub-state definition

    typedef enum { STATE_PARENT, STATE_CHILD } State; // with STATE_CHILD nested inside STATE_PARENT logically correctly states that STATE_CHILD is logically nested inside STATE_PARENT, even though enums are flat.
  3. Final Answer:

    typedef enum { STATE_PARENT, STATE_CHILD } State; // with STATE_CHILD nested inside STATE_PARENT logically -> Option C
  4. Quick Check:

    Logical nesting, not code nesting [OK]
Hint: Enums are flat; nesting is logical, not syntax [OK]
Common Mistakes:
  • Trying to nest enums inside enums (not allowed in C)
  • Assuming code blocks define sub-states
  • Declaring sub-states as separate enums without relation
3. Given the following code snippet for a hierarchical state machine, what will be the output when event EVENT_START occurs in STATE_IDLE?
#define EVENT_START 1
typedef enum { STATE_IDLE, STATE_ACTIVE, STATE_ACTIVE_SUB } State;
State current_state = STATE_IDLE;

void handle_event(int event) {
  switch (current_state) {
    case STATE_IDLE:
      if (event == EVENT_START) {
        current_state = STATE_ACTIVE;
        printf("Entered ACTIVE state\n");
      }
      break;
    case STATE_ACTIVE:
      if (event == EVENT_START) {
        current_state = STATE_ACTIVE_SUB;
        printf("Entered ACTIVE_SUB state\n");
      }
      break;
    default:
      printf("No action\n");
  }
}

handle_event(EVENT_START);
medium
A. No action
B. Entered ACTIVE state
C. Entered ACTIVE_SUB state
D. Compilation error

Solution

  1. Step 1: Check initial state and event

    The current_state is STATE_IDLE. The event is EVENT_START.
  2. Step 2: Follow switch-case logic for STATE_IDLE

    On EVENT_START in STATE_IDLE, current_state changes to STATE_ACTIVE and prints "Entered ACTIVE state".
  3. Final Answer:

    Entered ACTIVE state -> Option B
  4. Quick Check:

    STATE_IDLE + EVENT_START = Entered ACTIVE state [OK]
Hint: Trace current state before event handling [OK]
Common Mistakes:
  • Assuming it jumps directly to sub-state
  • Ignoring initial state value
  • Confusing event handling in different states
4. Identify the error in this hierarchical state machine event handler code snippet:
#define EVENT_GO 1
typedef enum { STATE_TOP, STATE_SUB } State;
State current_state = STATE_TOP;

void handle_event(int event) {
  switch (current_state) {
    case STATE_TOP:
      if (event = EVENT_GO) {
        current_state = STATE_SUB;
        printf("Moved to SUB state\n");
      }
      break;
    case STATE_SUB:
      printf("In SUB state\n");
      break;
  }
}
medium
A. Using assignment (=) instead of comparison (==) in if condition
B. Missing default case in switch statement
C. Incorrect enum values for states
D. No return statement in function

Solution

  1. Step 1: Examine if condition in STATE_TOP case

    The condition uses single '=' which assigns EVENT_GO to event instead of comparing.
  2. Step 2: Understand impact of assignment in if

    This causes the condition to always be true if EVENT_GO is non-zero, leading to unintended behavior.
  3. Final Answer:

    Using assignment (=) instead of comparison (==) in if condition -> Option A
  4. Quick Check:

    Use '==' for comparison, not '=' [OK]
Hint: Check if conditions for == vs = errors [OK]
Common Mistakes:
  • Confusing assignment and comparison operators
  • Ignoring missing default case (not critical here)
  • Expecting return in void function
5. You want to design a hierarchical state machine in embedded C where STATE_RUNNING has two sub-states: STATE_RUNNING_FAST and STATE_RUNNING_SLOW. Which approach correctly models this hierarchy and allows shared entry actions for STATE_RUNNING and specific actions for its sub-states?
hard
A. Define separate enums for all states and use nested switch-case to handle parent and sub-states with shared and specific actions.
B. Use a single enum with flat states and duplicate code for shared actions in each sub-state case.
C. Use multiple variables to track parent and sub-state separately without nesting logic.
D. Implement sub-states as independent states without any relation to the parent state.

Solution

  1. Step 1: Understand hierarchical state machine design

    Parent states group sub-states to share common behavior and allow specific sub-state actions.
  2. Step 2: Identify correct coding approach

    Defining separate enums for parent and sub-states and using nested switch-case allows shared entry actions for parent and specific actions for sub-states efficiently.
  3. Final Answer:

    Define separate enums for all states and use nested switch-case to handle parent and sub-states with shared and specific actions. -> Option A
  4. Quick Check:

    Nested switch-case models hierarchy best [OK]
Hint: Use nested switch-case for parent and sub-state actions [OK]
Common Mistakes:
  • Duplicating shared actions in sub-states
  • Tracking states separately without hierarchy
  • Ignoring parent-child relationship in code
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