Special method for connecting strings by # define [go]

Source: Internet
Author: User
Tags define null

In # define, the standard defines only # and # #两种操作. #用来把参数转换成字符串, # #则用来连接两个前后两个参数, turn them into a string.

#define CONN (x, y) x# #y//connection X, Y

#define TOCHAR (x) #@x//Plus Single quote

#define TOSTRING (x) #x//plus double quotation marks

Recently read the COM related information, see CCmdTarget Implement COM interface, read the definition of some macros, in the Afxdisp.h header file

#define Begin_interface_part (Localclass, baseclass) \
Class x# #localClass: Public baseclass \

Originally this macro definition is easy to understand, but there is a x## here, I really have not seen this usage, do not know what it is intended.
Later asked a few friends also do not know.

You know what?

Perhaps you do not know ~ hehe, at last I still found the relevant information, interpretation of this define, but also by the way to know the other two less commonly used define

#define CONN (x, y) x# #y
#define TOCHAR (x) #@x
#define TOSTRING (x) #x

x# #y表示什么? Represents x connection Y, for example:
int n = Conn (123,456); The result is n=123456;
char* str = Conn ("asdf", "ADF") result is str = "ASDFADF";
It's amazing, isn't it?

To see #@x, in fact, is to add single quotation marks to x, the result is a const char. For example,
char a = ToChar (1); The result is a= ' 1 ';
Do a cross-border test char a = TOCHAR (123); The result is a= ' 3 ';
But if your parameter exceeds four characters, the compiler will give you an error! Error C2015:too many characters in Constant:p

Finally look at #x, and you know, he's giving X double quotes.
char* str = ToString (123132); str= "123132";

Finally leave a few small experiments for everyone to test:
#define DEC (x, y) (X-y)
int n = Dec (A (123,1), 1230);
n = Conn (123, Conn (123,332));
char* str = A ("n", ToString (Dec (3,1));
What would be the result? Hey Hey hehe ~


Some of the other assorted things:
I defined a macro first.
#define TE Text
I want to define another macro that will change Te to a string ("text").
That is defined:
#define STR (str)??? can be replaced with "text" when using str (TE).
Excuse me??? How does the part come about? The use of #str is not up to the goal, with ' ' # #str # # ' is also wrong.

#不能做二次替換, you can define a medium macro again
#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,define not a simple substitution of characters in the arr array?

#define A "AAA"
#define B "BBB" A
I'm not familiar with macro this thing ... However this can succeed printf ("%s\n", B);


In # define, the standard defines only # and # #两种操作. #用来把参数转换成字符串, # #则用来连接两个前后两个参数, turn 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;
}
Output to
[Email protected] src]$./a.out
Token 9 = 10

To add: When defining with # define, you can use a slash ("\") to continue the line. With the underscore ("_") in VB.
Like what:
#define ADD1 (x, y) (x + y)
Can also be expressed as:
#define ADD1 (x, y) \
(x + y)

3 General Usage
1.
C: #define X----> C + +: const int x=100;

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

3.
#ifndef NULL
#define NULL ((void*) 0)
#endif

#ifndef _myheadfile_h
#define _myheadfile_h
#endif

Special method for connecting strings by # define [go]

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.