Event System
Part of the Object Oriented Programming section of Coddy's C journey — lesson 61 of 61.
Challenge
EasyLet'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 theHandlerfunction pointer type that takes aconst char*message. Create anEventstruct 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 0subscribe— takes an Event pointer and a Handler, adds the handler to the array if there's room (count less than 5), and increments the countfire— 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 messagealert— prints[ALERT]followed by the messagecounter— uses a static variable to count how many times it's been called, then prints[COUNT] Xwhere X is the current count
Your program will receive:
- The number of handlers to subscribe (1, 2, or 3)
- The message to broadcast
Example output when inputs are 3 and System started:
[LOG] System started
[ALERT] System started
[COUNT] 1Example output when inputs are 2 and Warning detected:
[LOG] Warning detected
[ALERT] Warning detectedExample output when inputs are 1 and Hello:
[LOG] HelloThis 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;
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Modular Programming Basics
Header FilesInclude GuardsSource FilesStatic FunctionsRecap: Modular Calculator4Encapsulation
Opaque Pointers ConceptDefining Opaque StructsGetters and SettersValidation in SettersRecap: Secret Box2Objects and Methods
Structs as ObjectsThe 'Self' PointerConst CorrectnessPointer vs ValueHelper MethodsRecap: Point Manager5Project: Simple Bank Account
Project SetupImplementation of Account3Object Lifecycle
Constructor PatternDestructor PatternStack InitializationDeep CopyRecap: String Wrapper6Inheritance via Composition
Struct EmbeddingThe First Member RuleAccessing Parent MembersUpcastingRecap: Shape Hierarchy