0
0
Embedded Cprogramming~20 mins

State machine for protocol handling in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Protocol 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 protocol state machine

What is the output of this embedded C code simulating a simple protocol state machine?

Embedded C
#include <stdio.h>

typedef enum {IDLE, WAIT_ACK, DONE} State;

int main() {
    State current_state = IDLE;
    int event = 1; // 1 means send request

    switch(current_state) {
        case IDLE:
            if(event == 1) {
                printf("Request sent\n");
                current_state = WAIT_ACK;
            }
            break;
        case WAIT_ACK:
            printf("Waiting for ACK\n");
            current_state = DONE;
            break;
        case DONE:
            printf("Protocol complete\n");
            break;
    }
    return 0;
}
ARequest sent
BWaiting for ACK
CProtocol complete
DNo output
Attempts:
2 left
💡 Hint

Look at the initial state and which case runs first.

Predict Output
intermediate
2:00remaining
State transition count in protocol handler

What is the value of count after running this code?

Embedded C
#include <stdio.h>

typedef enum {START, PROCESS, END} State;

int main() {
    State s = START;
    int count = 0;

    while(s != END) {
        switch(s) {
            case START:
                count++;
                s = PROCESS;
                break;
            case PROCESS:
                count += 2;
                s = END;
                break;
            default:
                break;
        }
    }
    printf("%d\n", count);
    return 0;
}
A1
B2
C3
D0
Attempts:
2 left
💡 Hint

Count how many times the loop runs and what increments happen.

🔧 Debug
advanced
2:00remaining
Identify the error in this protocol state machine code

What error does this code produce when compiled?

Embedded C
#include <stdio.h>

typedef enum {INIT, RUNNING, STOPPED} State;

int main() {
    State s = INIT;

    switch(s) {
        case INIT:
            printf("Init state\n");
            s = RUNNING;
            break;
        case RUNNING:
            printf("Running state\n");
            break;
        case STOPPED:
            printf("Stopped state\n");
            break;
    }
    return 0;
}
ASyntaxError: missing semicolon after s = RUNNING
BTypeError: cannot assign enum to int
CRuntime error: invalid state
DNo error, runs fine
Attempts:
2 left
💡 Hint

Check each line carefully for missing punctuation.

📝 Syntax
advanced
2:00remaining
Which option causes a compilation error in this state machine snippet?

Given the following snippet, which option will cause a compilation error?

typedef enum {S1, S2, S3} State;

State current = S1;

switch(current) {
    case S1:
        // do something
        break;
    case S2:
        // do something else
        break;
    case S3:
        // final state
        break;
}
Acase S1: current = S2; break;
Bcase S2: current = S3; break;
Ccase S1: current = 1; break;
Dcase S3: current = 5; break;
Attempts:
2 left
💡 Hint

Consider the enum values and what assignments are valid.

🚀 Application
expert
3:00remaining
Number of states visited in a protocol simulation

Consider this protocol state machine code. How many states will be visited (printed) before the program ends?

Embedded C
#include <stdio.h>

typedef enum {START, SEND, RECEIVE, END} State;

int main() {
    State s = START;
    int count = 0;

    while(s != END) {
        switch(s) {
            case START:
                printf("START\n");
                s = SEND;
                count++;
                break;
            case SEND:
                printf("SEND\n");
                s = RECEIVE;
                count++;
                break;
            case RECEIVE:
                printf("RECEIVE\n");
                s = END;
                count++;
                break;
            default:
                break;
        }
    }
    printf("States visited: %d\n", count);
    return 0;
}
A4
B3
C2
D1
Attempts:
2 left
💡 Hint

Count how many times the loop prints and increments count before reaching END.