You may not know # define usage: concatenate a string through # define

Source: Internet
Author: User
Tags define null
I recently read the com-related documents. When I saw that csf-target implemented the COM interface, I read some macro definitions in the afxdisp. h header file.

# Define begin_interface_part (localclass, baseclass )\
Class X # localclass: Public baseclass \

This macro definition is easy to understand, but here is an extra X #. I have never seen such a definition, and I don't know what it means.
I asked a few friends, but I didn't know either.

Do you know?

Maybe you don't know ~ Well, I finally found the relevant materials, explained the define, and also met the other two less commonly used define

# Define conn (x, y) x # Y
# Define tochar (x) # @ x
# Define tostring (x) # x

X ## what does y mean? X connects to Y, for example:
Int n = conn (123,456); the result is n = 123456;
Char * STR = conn ("ASDF", "ADF") returns STR = "asdfadf ";
How about it? It's amazing.

Let's take a look at # @ X. In fact, a single quotation mark is added to X, and the returned result is a const char. For example:
Char A = tochar (1); the result is a = '1 ';
Make an out-of-bounds test char a = tochar (123); the result is a = '3 ';
However, if your parameter contains more than four characters, the compiler reports an error! Error c2015: too failed
Characters in constant: P

Finally, let's take a look at # X. You may also understand that it adds double quotation marks to X.
Char * STR = tostring (123132); then STR = "123132 ";

Finally, let's leave a few small tests for everyone to test:
# Define Dec (x, y) (x-y)
Int n = Dec (A (1230 );
N = conn (123, Conn (123,332 ));
Char * STR = a ("12", tostring (Dec (3, 1 ));
What will happen? Hey hey ~

Other things:
I first defined a macro
# Define te text
I want to define another macro to convert te into a string ("text ").
Definition:
# Define STR (STR )??? // Replace STR (TE) with "text ".
Excuse me ??? How to implement this part? # STR cannot achieve the goal. '"' # STR # '"' is also incorrect.

# Secondary replacement is not allowed, and an intermediate macro can be defined.
# Define te text
# Define ad te
# Define STR (AD) # ad
Try

Define two macros directly!
# Define te text
# Define te_s "text"

# Define STR ABC
Char arr [10] = "str ";
Why is str rather than ABC in the ARR array? Isn't define a simple replacement of characters?

# Define a "AAA"
# Define B "BBB"
I'm not familiar with Macro ...... However, printf ("% s \ n", B) can be successful );

In # define, only the # And # operations are defined. # Used to convert parameters into strings. # used to connect two parameters before and after, and convert them into a string.
# Include <stdio. h>
# Define Paster (n) printf ("token" # N "= % d \ n", Token # N)
Int main ()
{
Int token9 = 10;
Paster (9 );
Return 0;
}
The output is
[Leshy @ leshy SRC] $./A. Out
Token 9 = 10

Additionally, when # define is used for definition, you can use a slash ("\") to continue the line ("
_ ") Same.
For example:
# Define Add1 (x, y) (x + y)
It can also be expressed:
# Define Add1 (x, y )\
(X + y)

Three common usage
1.
C: # define x 100 ----> C +++: const int x = 100;

2.
C: # define max (A, B) (a)> (B )? (A) (B ))
C ++: inline int max (int A, int B) {return A> B? A: B ;}

3.
# Ifndef null
# Define null (void *) 0)

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.