C Language & quot ;#& quot;, & quot ;#@& quot; and & quot ;##

Source: Internet
Author: User

The C language is profound and profound, and every detail contains many technical features. I believe that the first time C programmers encounter skills such as "#", "# @" and "#", it is also yunyun. This article summarizes the usage tips of "#", "# @" and "#" in macro definition and collects them on the Internet! For reference when necessary, and hope to help other peers.

I. general usage

# Convert the macro parameter to a string, # @ convert the macro parameter to a character, and # combine the two macro parameters.

# Include <stdio. h>

# Include <limits. h>

# Define STR (s) # s // # There can be spaces between parameters

# Define TOCHAR (c) # @ c

# Define CONS (a, B) int (a # e # B) // # There can be spaces between parameters

Int main (void)

{

Printf (STR (pele); // output string "pele"

Printf ("% c \ n", TOCHAR (z); // output character z

Printf ("% d \ n", CONS (2000); // 2e3 output:

Return 0;

}

2. When the macro parameter is another macro

Note that all macro parameters that use '#' or '#' in macro definition will not be expanded.

# Define A (2)

# Define STR (s) # s

# Define CONS (a, B) int (a ## e ## B)

 

Printf ("int max: % s \ n", STR (INT_MAX ));

This row is expanded:

Printf ("int max: % s \ n", "INT_MAX ");

Printf ("% s \ n", CONS (A, ));

This row is expanded:

Printf ("% s \ n", int (AeA ));

 

Both INT_MAX and A will not be expanded. However, the solution to this problem is simple. Add an intermediate conversion macro. The purpose of adding this macro layer is to expand all macro parameters in this layer, so that the macro (_ STR) in the conversion macro can get the correct macro parameters.

# Define A (2)

# Define _ STR (s) # s

# Define STR (s) _ STR (s) // convert macro

# Define _ CONS (a, B) int (a ## e ## B)

# Define CONS (a, B) _ CONS (a, B) // convert a macro

 


Printf ("int max: % s \ n", STR (INT_MAX ));

Output: int max: 0x7fffffff

STR (INT_MAX) --> _ STR (0x7fffffff) --> "0x7fffffff"

 

Printf ("% d \ n", CONS (A, ));

Output: 200

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

 

The Source Code related to the Minix operating system is as follows:

# Ifdef _ ANSI

# Define _ str (x) # x

# Define _ xstr (x) _ str (x) // convert a macro

_ PROTOTYPE (void _ bad_assertion, (const char * _ mess ));

# Define assert (expr )? (Void) 0 :\

_ Bad_assertion ("Assertion \" "# expr \

"\" Failed, file "_ xstr (_ FILE __)\

", Line" _ xstr (_ LINE _) "\ n "))

Iii. Application exceptions 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 __)

For example, ANONYMOUS (static int); that is, static int _ anonymous70; 70 indicates the row number;

First layer: ANONYMOUS (static int); --> _ ANONYMOUS0 (staticint, _ LINE __);

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

Layer 3: --> static int _ anonymous70;

That is, the macro of the current layer can only be unlocked at each time, so _ LINE _ can be unlocked at the second layer;

 

2. fill Structure

# Define FILL (a) {a, #}

Enum IDD {OPEN, CLOSE };

Typedef struct MSG

{

IDD id;

Const char * msg;

} MSG;

MSG _ msg [] = {FILL (OPEN), FILL (CLOSE )};

Equivalent:

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) // convert a macro

Static char FILE_NAME [] = GET_FILE_NAME (_ FILE __);

 

4. Obtain the buffer size of the string corresponding to the value 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:

Char buf [11];

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.