Practical C Programs
Command Router
Command-line style programs often map a selected command to one compact action path.
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.
Command Router
command_router.c
Replay: real traced execution (multi-file project)
#include <stdio.h>
int main(void) {
int commandChoice = 1;
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 = 0;
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 = 2;
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;
}
commandChoice ← 1, command ← build, status ← 0
3int main(void) {4 int commandChoice→ 1 = 1; //@commandChoice=0, 25 const char *command→ build = "build";6 int status→ 0 = 0;78 if (commandChoice == 0) {9 command = "test";10 status = 0;11 } else if (commandChoice == 2) {12 command = "deploy";13 status = 2;14 }1516 printf("choice=%d command=%s status=%d\n", commandChoice1, commandbuild, status0);17 return 0;18}outputchoice=1 command=build status=0
commandChoice ← 0, command ← build, status ← 0
3int main(void) {4 int commandChoice→ 0 = 0;5 const char *command→ build = "build";6 int status→ 0 = 0;command ← test
8if (commandChoice0 == 0) {9 command→ test = "test";10 status0 = 0;11} else if (commandChoice == 2) {printf("choice=%d command=%s status=%d ", commandChoice, command, stat…
16 printf("choice=%d command=%s status=%d\n", commandChoice0, commandtest, status0);17 return 0;18}outputchoice=0 command=test status=0
commandChoice ← 2, command ← build, status ← 0
3int main(void) {4 int commandChoice→ 2 = 2;5 const char *command→ build = "build";6 int status→ 0 = 0;command ← deploy, status ← 2
10 status = 0;11} else if (commandChoice2 == 2) {12 command→ deploy = "deploy";13 status→ 2 = 2;14}printf("choice=%d command=%s status=%d ", commandChoice, command, stat…
16 printf("choice=%d command=%s status=%d\n", commandChoice2, commanddeploy, status2);17 return 0;18}outputchoice=2 command=deploy status=2