C language define concatenates macro definition implementation

  • 2020-06-23 01:29:34
  • OfStack

How to use it: Splice two macros, one passing in. But instead of replacing the incoming macros, the incoming macros are connected intact, which is awkward. After all kinds of attempts, it has become. I hereby record and share 1 for your convenience to learn.


char A_param=0;

char B_pramm=0;

// Adding macro definitions 

#define OBJECT     A 
#define DEFINE_(X)  X##_param    //1 Once defined 
#define DEFINE(X)   DEFINE_(X) // Define again 
#define PARAM  DEFINE(OBJECT)

void fun()
{

// DEFINE_(OBJECT)=100;  This operation is rejected, it is simply spliced without being replaced 

  DEFINE(OBJECT)=100; // That's the magic. Replace and splice. We just did it 2 Sub-definition, solution 1 Cut the trouble 

  PARAM=100;// That's ok 

}

Does that ultimately achieve your goal? The essential principle is probably the sequence of substitution and splicing, which I have not studied carefully.

Here is one of my USES.


#define STEP_TIMx 4//TIM4
#define STEP_CHx 2//CH2

#define SET_STEP42_PPS_1(NUM,X) TIM##NUM->PSC=((X))

#define SET_STEP42_PPS_2(NUM,X) SET_STEP42_PPS_1(NUM,X)

#define SET_STEP42_PPS(X)   SET_STEP42_PPS_2(STEP_TIMx,X)

// The ultimate goal is to achieve  TIM4->PSC=X , My purpose was accomplished 

Related articles: