Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use # and # # special symbols in C language macros

Shulou Source: shulou.com Published: 2022-06-01 04:21:16 09月30日 Update

This article will explain in detail how to use # and # # special symbols in C language macros. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

The usage of "#" and "#" in macros

First, use "#" to change the macro parameters into a string, and use "#" to fit the two macro parameters together.

# define STR (val) (# val) # define CONS (a, b) (int) (a##e##b)-> STR (hello) = > "hello" CONS (2,3) = > 2000 / / 2e3

Second, when the macro parameter is another macro

It is important to note that where the macro definition is useful'#'or'# 'macro parameters are no longer expanded.

1, non -'# 'and' # 'cases

# define TOW (2) # define MUL (aPowerb) (asancb) printf ("% d*%d=%d\ n", TOW,TOW, MUL (TOW,TOW)); = > printf ("% d*%d=%d\ n", (2), (2), (2) * (2)

The parameter TOW in MUL will be expanded to (2).

2, when there is a'#'or'# #'

# define A (2) # define STR (s) # s # define CONS (define STR b) int (a##e##b) printf ("int max:% s\ n", STR (INT_MAX)); / / INT_MAX # include = > printf ("int max:% s\ n", "INT_MAX"); printf ("% s\ n", CONS (A, A)); / / compile error = > printf ("% s\ n", int (AeA))

INT_MAX and A will no longer be expanded, but the solution to this problem is very simple: add an additional layer of intermediate conversion macros, which is intended to expand all the macro parameters in this layer, then the macro in the conversion macro (_ STR) can get the correct macro parameters.

# define A (2) # define _ STR (s) (# s) # define STR (s) _ STR (s) / conversion macro # define _ CONS (amemb) int (a##e##b) # define CONS (ameme b) _ CONS (amemb) / / convert macro printf ("int max:% s\ n", STR (INT_MAX)); / / INT_MAX, the maximum of int type, is a variable # include

The output is: int max: 0x7fffffff

STR (INT_MAX)-- > _ STR (0x7fffffff) and then converted to a string; printf ("% d\ n", CONS (A, A))

Output: 200

CONS (A, A)-- > _ CONS ((2), (2)-- > int ((2) e (2))

Third, some special cases of'# 'and' #'

1. Merge anonymous variable names

# define _ _ ANONYMOUS1 (type, var, line) type var##line # define _ _ ANONYMOUS0 (type, line) _ _ ANONYMOUS1 (type, _ anonymous, line) # define ANONYMOUS (type) _ ANONYMOUS0 (type, _ LINE__)

Example: ANONYMOUS (static int)

That is, static int _ anonymous70; 70 represents the line number

First floor: ANONYMOUS (static int)

-- > _ _ ANONYMOUS0 (static int, _ LINE__)

Layer 2:-- > _ ANONYMOUS1 (static int, _ anonymous, 70)

Layer 3:-- > static int _ anonymous70

That is, you can only unlock the macro of the current layer at a time, so the _ _ LINE__ can only be unlocked at the second layer.

2. Filling structure

# define FILL (a) {a, # a} enum IDD {OPEN, CLOSE}; typedef struct MSG {IDD id; const char * msg;} MSG; MSG _ msg [] = {FILL (OPEN), FILL (CLOSE)}; = > MSG _ msg [] = {{OPEN, "OPEN"}, {CLOSE, "CLOSE"}}

3. Record file name

# define _ GET_FILE_NAME (f) # f # define GET_FILE_NAME (f) _ GET_FILE_NAME (f) static char FILE_NAME [] = GET_FILE_NAME (_ _ FILE__)

4. Get the string buffer size corresponding to a numeric type

# define _ TYPE_BUF_SIZE (type) sizeof # type # define TYPE_BUF_SIZE (type) _ TYPE_BUF_SIZE (type) char buf [type _ BUF_SIZE (INT_MAX)];-- > char buf [_ TYPE_BUF_SIZE (0x7fffffff)];-- > char buf [sizeof "0x7fffffff"]

This is equivalent to: char buf [11]

This is the end of the article on "how to use # and # # special symbols in C language macros". I hope the above content can be helpful to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.

Tags: Parameters characters strings articles special symbols languages two levels variables time more output good practical maximum fit two content place size Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple vpn Shulou Tech Info Linux Docker