Deutsch   English   Français   Italiano  
<vr2rl7$2jboh$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder9.news.weretis.net!news.quux.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: James Kuyper <jameskuyper@alumni.caltech.edu>
Newsgroups: comp.lang.c
Subject: Re: Concatenated if and preprocessor
Date: Fri, 14 Mar 2025 23:20:07 -0400
Organization: A noiseless patient Spider
Lines: 64
Message-ID: <vr2rl7$2jboh$1@dont-email.me>
References: <vquuhg$34o8d$2@dont-email.me> <vr15ti$rtjs$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 15 Mar 2025 04:20:18 +0100 (CET)
Injection-Info: dont-email.me; posting-host="574a59b4fab7f6890a5b81a9d0258b48";
	logging-data="2731793"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX186i2giUeb7GsgYWuuxK2hv7Be2fQRSQyE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:qHK2sPuxCCliSwh0R9G6GsJc0B0=
In-Reply-To: <vr15ti$rtjs$1@dont-email.me>
Content-Language: en-US
Bytes: 3004

On 3/14/25 08:02, pozz wrote:
> 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 {
>    ...
> }

If all of the if-conditions are tests of the same variable for different
values, switch() is the more appropriate control structure t than
repeated "if() {} else". In that case, the feature macros can simply
control the cases that exist within the switch statement.
If switch is not suitable, it must be because of some complication you
haven't shown us yet.