preprocessor-toolchain · beginner · ~15 min
Configure software profile metrics and compile-time boundaries cleanly with enums and directives.
Conditional compilation directives (#ifdef, #ifndef, #if, #elif, #else) allow a single C codebase to target different environments, hardware profiles, and security levels without runtime overhead.
Implement a compile-time feature evaluator:
typedef enum {
PROFILE_DEBUG = 1,
PROFILE_RELEASE = 2,
PROFILE_EMBEDDED = 3
} SystemProfile;
int get_buffer_capacity_for_profile(SystemProfile profile);
const char *get_profile_name(SystemProfile profile);
get_buffer_capacity_for_profile:PROFILE_DEBUG: returns 1024 * 64 (65536)PROFILE_RELEASE: returns 1024 * 16 (16384)PROFILE_EMBEDDED: returns 512-1get_profile_name:PROFILE_DEBUG: returns "DEBUG"PROFILE_RELEASE: returns "RELEASE"PROFILE_EMBEDDED: returns "EMBEDDED""UNKNOWN"get_buffer_capacity_for_profile(PROFILE_EMBEDDED); // returns 512
get_profile_name(PROFILE_DEBUG); // returns "DEBUG"
profile: SystemProfile enum value.
Returns buffer capacity integer and static string name.
Zero dynamic allocations. Static string returns.
typedef enum {
PROFILE_DEBUG = 1,
PROFILE_RELEASE = 2,
PROFILE_EMBEDDED = 3
} SystemProfile;
int get_buffer_capacity_for_profile(SystemProfile profile) {
(void)profile;
return -1;
}
const char *get_profile_name(SystemProfile profile) {
(void)profile;
return "UNKNOWN";
}
Missing break statements in switch-case; returning pointer to local stack variable.
Unknown profile returns -1 for capacity and UNKNOWN for name.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.