Implementation of C language #define splicing macro definition

Time:2022-5-2

Usage: splice two macros, one is the incoming macro. But the incoming macro will not be replaced. Instead, it will be connected intact, which is embarrassing. After various attempts, it turned out to be. I hereby record and share it for your convenience.

char A_param=0;

char B_pramm=0;

//Add macro definition

#define OBJECT     A 
#define DEFINE_ (X)  X##_ Param // once defined
#define DEFINE(X)   DEFINE_ (10) // define again
#define PARAM  DEFINE(OBJECT)

void fun()
{

// DEFINE_ (OBJECT)=100;  This operation is rejected. It is a direct splicing and will not be replaced

  DEFINE(OBJECT)=100; // This operation is divine. Replace first and then splice. We just make a secondary definition to solve all our troubles

  PARAM=100;// This is also possible

}

Will you achieve your goal in the end? The essential principle is probably the order of replacement and splicing. I haven’t studied it in detail.

The following is an occasion for me to use.

#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 goal has been achieved

The above implementation method of C language #define splicing macro definition is all the content shared by Xiaobian. I hope it can give you a reference and support developeppaer.