preprocessor-toolchain · beginner · ~15 min

Compile-Time Feature Gate

Configure software profile metrics and compile-time boundaries cleanly with enums and directives.

Challenge

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.

Your Task

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);

Rules

  1. get_buffer_capacity_for_profile:
    • PROFILE_DEBUG: returns 1024 * 64 (65536)
    • PROFILE_RELEASE: returns 1024 * 16 (16384)
    • PROFILE_EMBEDDED: returns 512
    • Unrecognized profile: returns -1
  2. get_profile_name:
    • PROFILE_DEBUG: returns "DEBUG"
    • PROFILE_RELEASE: returns "RELEASE"
    • PROFILE_EMBEDDED: returns "EMBEDDED"
    • Unrecognized: returns "UNKNOWN"

Example

get_buffer_capacity_for_profile(PROFILE_EMBEDDED); // returns 512
get_profile_name(PROFILE_DEBUG); // returns "DEBUG"

Input format

profile: SystemProfile enum value.

Output format

Returns buffer capacity integer and static string name.

Constraints

Zero dynamic allocations. Static string returns.

Starter code

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";
}

Common mistakes

Missing break statements in switch-case; returning pointer to local stack variable.

Edge cases to handle

Unknown profile returns -1 for capacity and UNKNOWN for name.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.