0
0
Embedded Cprogramming~20 mins

Simple state machine with switch-case in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 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
What is the output of this simple state machine code snippet?
Embedded C
#include <stdio.h>

int main() {
    enum State {IDLE, RUNNING, STOPPED} state = IDLE;
    switch(state) {
        case IDLE:
            printf("Idle state\n");
            break;
        case RUNNING:
            printf("Running state\n");
            break;
        case STOPPED:
            printf("Stopped state\n");
            break;
    }
    return 0;
}
AIdle state
BRunning state
CStopped state
DNo output
Attempts:
2 left
💡 Hint
Look at the initial value of the state variable.
Predict Output
intermediate
2:00remaining
State transition output
What will be printed after this code runs?
Embedded C
#include <stdio.h>

int main() {
    enum State {IDLE, RUNNING, STOPPED} state = RUNNING;
    switch(state) {
        case IDLE:
            printf("Idle\n");
            break;
        case RUNNING:
            printf("Running\n");
            state = STOPPED;
            break;
        case STOPPED:
            printf("Stopped\n");
            break;
    }
    switch(state) {
        case IDLE:
            printf("Idle\n");
            break;
        case RUNNING:
            printf("Running\n");
            break;
        case STOPPED:
            printf("Stopped\n");
            break;
    }
    return 0;
}
A
Running
Stopped
B
Running
Running
C
Stopped
Stopped
D
Idle
Stopped
Attempts:
2 left
💡 Hint
Check how the state variable changes after the first switch.
🔧 Debug
advanced
2:00remaining
Identify the error in this state machine code
What error will this code produce when compiled?
Embedded C
#include <stdio.h>

int main() {
    enum State {IDLE, RUNNING, STOPPED} state = IDLE;
    switch(state) {
        case IDLE
            printf("Idle\n");
            break;
        case RUNNING:
            printf("Running\n");
            break;
        case STOPPED:
            printf("Stopped\n");
            break;
    }
    return 0;
}
ARuntime error: uninitialized variable
BSyntax error: missing colon after case IDLE
CNo error, prints 'Idle'
DLogic error: wrong output
Attempts:
2 left
💡 Hint
Check the syntax of the case labels in the switch statement.
Predict Output
advanced
2:00remaining
Output with fall-through in switch-case
What will this program print?
Embedded C
#include <stdio.h>

int main() {
    enum State {IDLE, RUNNING, STOPPED} state = IDLE;
    switch(state) {
        case IDLE:
            printf("Idle\n");
        case RUNNING:
            printf("Running\n");
            break;
        case STOPPED:
            printf("Stopped\n");
            break;
    }
    return 0;
}
ARunning
BIdle
C
Idle
Running
DStopped
Attempts:
2 left
💡 Hint
Notice the missing break after case IDLE.
🧠 Conceptual
expert
2:00remaining
Number of states handled by this state machine
Given this state machine code, how many distinct states does it handle?
Embedded C
#include <stdio.h>

int main() {
    enum State {IDLE = 0, RUNNING = 1, PAUSED = 2, STOPPED = 3} state = PAUSED;
    switch(state) {
        case IDLE:
            printf("Idle\n");
            break;
        case RUNNING:
        case PAUSED:
            printf("Active\n");
            break;
        case STOPPED:
            printf("Stopped\n");
            break;
    }
    return 0;
}
A4
B1
C2
D3
Attempts:
2 left
💡 Hint
Count how many unique cases produce output.