Home > front end >  Is it possible to have #ifdef,#endif in functions's argument in c?
Is it possible to have #ifdef,#endif in functions's argument in c?

Time:09-30

Hello I wanted to know if it is possible to have preprocessor language ine the argument of a function.

Something like this :

static void OnTxData( 
#ifdef MODULE1
    TxParams_t* params
#else
    Tx2Params_t* params
#endif
                       )
{
...
}

CodePudding user response:

Yes. You can use these literally anywhere (as long as they go on their own line). The preprocessor "works these out" before the rest of the compiler compiles anything.

So if MODULE1 is defined then this is the code that gets compiled:

static void OnTxData( 
    TxParams_t* params
                       )
{
...
}

and if it's not defined, then this is the code that gets compiled:

static void OnTxData( 
    Tx2Params_t* params
                       )
{
...
}

CodePudding user response:

Instead of hard to read:

static void OnTxData( 
#ifdef MODULE1
    TxParams_t* params
#else
    Tx2Params_t* params
#endif
                       )
{
...
}

#ifdef MODULE1
    #define TX_Params TxParams_t
#else
    #define TX_Params Tx2Params_t
#endif

static void OnTxData( TX_Params *prams)
{
...
}

or

#ifdef MODULE1
    typedef TxParams_t TX_Params
#else
    typedef Tx2Params_t TX_Params
#endif

static void OnTxData( TX_Params *prams)
{
...
}

or (IMO worse)

#ifdef MODULE1
static void OnTxData( TxParams_t *prams)
#else
static void OnTxData( Tx2Params_t *prams)
#endif
{
...
}
  • Related