And ## of the c # language usage

#include <stdio.h>

#define ADD(A,B) printf(#A " + " #B " = %d\n",((A)+(B)))
#define XNAME(n) x ## n
#define PRINT_XN(n) printf("x" #n " = %d\n", x ## n)


int main(void)
{
	ADD(4,5);

	int XNAME(1) = 14;
	PRINT_XN(1);

    return 0;
}

  Tips again after learning about the two also defined in vscode can put the mouse code, you can see the final results of the expanded macro definitions. This editor, it is recommended, I hit him with writing code.

1, if A is a macro formal parameter, #A is then converted to a string of "A" parameter names. This process is called string of (stringizing)

2, is similar to operator and # ## can be used for operator class macro function (parameterized macro) a replacement part. ## the operator can be combined into a two tokens token.

 

 

 

 

operation result:

 

 ## operator here did not seem to play much of convenience, but will make us feel not used. However, using the operator ## can sometimes improve the readability of encapsulation and procedures. The real-time OS running stm32 program on gpio hardware drivers have the following code:

 

 

#define __STM32_PIN(index, gpio, gpio_index) \
{ \
index, GPIO##gpio##_CLK_ENABLE, GPIO##gpio, GPIO_PIN_##gpio_index \
}

 

 

 

Assuming that such use of the macro:

 

__STM32_PIN(7, C, 13)

 

 

 

This macro expands to:

 

{7, GPIOC_CLK_ENABLE, GPIOC, GPIO_PIN_13}
This can then easily be used to list the hardware pins so to then take it to configure

 

 

 

Guess you like

Origin www.cnblogs.com/CodeWorkerLiMing/p/12044617.html