Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Concatenated if and preprocessor Date: Fri, 14 Mar 2025 09:44:16 -0700 Organization: A noiseless patient Spider Lines: 120 Message-ID: <86frjfsgtb.fsf@linuxsc.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Fri, 14 Mar 2025 17:44:16 +0100 (CET) Injection-Info: dont-email.me; posting-host="825d11fa9501a89399e86bed31f148c6"; logging-data="1735535"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18f12VHl5xKKx0oPIbDmSZ94XzS724aCN4=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:LQGjlIVu1FxPHZwi6zHeeSR7pPo= sha1:DWnjsRZ277NzYtPBhVZodLGCVJM= Bytes: 3842 pozz writes: > Il 13/03/2025 16:44, pozz ha scritto: > >> Consider this code: >> >> if (cond1) { >> ... >> } else if (cond2) { >> ... >> } else if (cond3) { >> ... >> } >> >> I want to activate every single if with a macro preprocessor. All >> the combinations are possible: only the first, only the second, only >> the third, the first and second... and so on. >> >> What's the best method to have a clean code that is always compiled >> without errors? > > You're right, a real example is better to define my problem. > > I have a project that can be compiled in different ways, depending on > the final product model (it's an embedded platform, it's a material > device). > I have three models with different characteristics: MODEL_A, MODEL_B, > MODEL_C. Three different builds that are managed at compile time by > defining MODEL macro as MODEL_A, MODEL_B or MODEL_C. > > For each model, I could have a different features set. FEATURE_1, > FUTURE_2, FUTURE_3, ... > > #if MODEL == MODEL_A > # define FEATURE_1 1 > # define FEATURE_2 0 > # define FEATURE_3 0 > #elif MODEL == MODEL_B > # define FEATURE_1 0 > # define FEATURE_2 1 > # define FEATURE_3 0 > #elif MODEL == MODEL_C > # define FEATURE_1 1 > # define FEATURE_2 1 > # define FEATURE_3 1 > #endif > > Now, on the full-featured model (MODEL_C) I could write: > > if (key == 1) { > ... > } else if (key == 2) { > ... > } else if (key == 3) { > ... > } else { > ... > } > > However, for MODEL_A I should have: > > if (key == 1) { > ... > } else { > ... > } > > For MODEL_B I should have: > > if (key == 2) { > ... > } else { > ... > } > > This is the scenario. > > I thought using if(0) or if(1), but many times I receive some > warnings from the compiler (condition is always true or condition > is always false). So your question is not a language question but how to avoid implemenation-specific warnings? It would have been good to explain that at the beginning. My first suggestion is to just turn off the damn warning. Any compiler that gives a "condition always false" warning on 'if(0)' is one best avoided. Having said that, here is an alternative: for( int just_once = 1; just_once; just_once = 0 ){ #if FEATURE_1 if( condition_1 ){ ... break; } #endif #if FEATURE_2 if( condition_2 ){ ... break; } #endif #if FEATURE_3 if( condition_3 ){ ... break; } #endif ... default code goes here ... } Test compiles at optimization level -O1 shows gcc and clang are both smart enough to optimize the for(...) out of existence, leaving just the enabled if() sections.