Practical C Programs
Command Router
Command-line style programs often map a selected command to one compact action path.
Command Router
command_router.c
#include <stdio.h>
int main(void) {
int commandChoice = ;
const char *command = "build";
int status = 0;
if (commandChoice == 0) {
command = "test";
status = 0;
} else if (commandChoice == 2) {
command = "deploy";
status = 2;
}
printf("choice=%d command=%s status=%d\n", commandChoice, command, status);
return 0;
}
#include <stdio.h>
int main(void) {
int commandChoice = ;
const char *command = "build";
int status = 0;
if (commandChoice == 0) {
command = "test";
status = 0;
} else if (commandChoice == 2) {
command = "deploy";
status = 2;
}
printf("choice=%d command=%s status=%d\n", commandChoice, command, status);
return 0;
}
#include <stdio.h>
int main(void) {
int commandChoice = ;
const char *command = "build";
int status = 0;
if (commandChoice == 0) {
command = "test";
status = 0;
} else if (commandChoice == 2) {
command = "deploy";
status = 2;
}
printf("choice=%d command=%s status=%d\n", commandChoice, command, status);
return 0;
}
command choice
The program turns a small integer choice into a command name and status code.
routing
Branches keep each command path explicit while sharing the same final reporting step.