Menu
Coddy logo textTech

Event System

Part of the Object Oriented Programming section of Coddy's C journey — lesson 61 of 61.

challenge icon

Challenge

Easy

Let's build a Notification System — an event-driven architecture where multiple handlers can subscribe to an event and respond when it fires. This pattern is the backbone of reactive programming and GUI frameworks.

You'll organize your code across three files:

  • event.h: Define the Handler function pointer type that takes a const char* message. Create an Event struct that holds an array of up to 5 handlers and a count of registered handlers. Declare functions for initializing an event, subscribing a handler, and firing the event to notify all subscribers.
  • event.c: Implement your event system:
    • init_event — takes an Event pointer and sets the handler count to 0
    • subscribe — takes an Event pointer and a Handler, adds the handler to the array if there's room (count less than 5), and increments the count
    • fire — takes an Event pointer and a message string, then calls each registered handler with that message in the order they were subscribed
  • main.c: Create three different handler functions that respond to events in unique ways:
    • logger — prints [LOG] followed by the message
    • alert — prints [ALERT] followed by the message
    • counter — uses a static variable to count how many times it's been called, then prints [COUNT] X where X is the current count
    Read an integer indicating how many handlers to subscribe (1, 2, or 3). Subscribe that many handlers in order: first logger, then alert (if 2+), then counter (if 3). Read a message string, then fire the event with that message.

Your program will receive:

  1. The number of handlers to subscribe (1, 2, or 3)
  2. The message to broadcast

Example output when inputs are 3 and System started:

[LOG] System started
[ALERT] System started
[COUNT] 1

Example output when inputs are 2 and Warning detected:

[LOG] Warning detected
[ALERT] Warning detected

Example output when inputs are 1 and Hello:

[LOG] Hello

This demonstrates polymorphism through function pointers — the same fire call triggers completely different behaviors depending on which handlers are subscribed. Each handler responds in its own way to the same event, showcasing the decoupled nature of event-driven design.

Try it yourself

#include <stdio.h>
#include <string.h>
#include "event.h"

// TODO: Implement logger handler
// Prints "[LOG] " followed by the message
void logger(const char* message) {
    // TODO: Implement
}

// TODO: Implement alert handler
// Prints "[ALERT] " followed by the message
void alert(const char* message) {
    // TODO: Implement
}

// TODO: Implement counter handler
// Uses a static variable to count calls
// Prints "[COUNT] X" where X is the current count
void counter(const char* message) {
    // TODO: Implement with static variable
}

int main() {
    // Read the number of handlers to subscribe
    int num_handlers;
    scanf("%d", &num_handlers);
    getchar(); // consume newline
    
    // Read the message
    char message[256];
    fgets(message, sizeof(message), stdin);
    message[strcspn(message, "\n")] = '\0'; // remove newline
    
    // TODO: Create and initialize an Event
    
    // TODO: Subscribe handlers based on num_handlers
    // 1 handler: logger
    // 2 handlers: logger, alert
    // 3 handlers: logger, alert, counter
    
    // TODO: Fire the event with the message
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming