WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C · Expert · question 75 of 100

How do you implement a Finite State Machine (FSM) in C, and what are its applications?

📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

A Finite State Machine (FSM) is a mathematical model used to represent and analyze complex systems that can be in one of a finite set of states at any given time. FSMs are commonly used in software engineering, digital logic design, and other fields to model and control the behavior of systems.

In C, an FSM can be implemented using a switch statement that selects an appropriate action based on the current state and the input received. The state of the FSM can be represented using a variable, and transitions between states can be modeled using conditional statements.

Here’s an example of an FSM that models a traffic light system:

    typedef enum {
        GREEN,
        YELLOW,
        RED
    } State;
    
    State traffic_light(State current_state, int input) {
        switch (current_state) {
            case GREEN:
            if (input == 1) {
                return YELLOW;
            } else {
                return GREEN;
            }
            case YELLOW:
            if (input == 1) {
                return RED;
            } else {
                return GREEN;
            }
            case RED:
            if (input == 1) {
                return GREEN;
            } else {
                return RED;
            }
            default:
            return RED;
        }
    }
    
    int main() {
        State current_state = GREEN;
        int input;
        
        while (1) {
            printf("Current state: %dn", current_state);
            printf("Enter input (1 or 0): ");
            scanf("%d", &input);
            current_state = traffic_light(current_state, input);
        }
        
        return 0;
    }

In this example, the FSM has three states (GREEN, YELLOW, and RED) that correspond to the different states of a traffic light. The traffic_light function takes the current state and an input (1 or 0) as arguments, and returns the next state based on the input and the current state. The main function repeatedly calls the traffic_light function with the current state and the user input, and updates the current state based on the result.

FSMs can be used to model a wide range of systems, from simple traffic lights to complex communication protocols. They provide a powerful and flexible tool for designing and implementing software that accurately reflects the behavior of the system it models.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C interview — then scores it.
📞 Practice C — free 15 min
📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

All 100 C questions · All topics