VC #define EC (x) l# #x的作用

Source: Internet
Author: User
Tags definition empty printf tostring

#define EC (x) l# #x role Description

Indicates L is connected with X.

#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") the result is str = "ASDFADF";

It's amazing, isn't it?

The #@x, in fact, is to add a single quotation mark to the X, and the result returns a const char. For example,

char a = ToChar (1); The result is a= ' 1 ';

Do a transboundary test. Char a = ToChar (123); The result is a= ' 3 ';

But if your argument is more than four characters, the compiler will give you an error! Error C2015:too many characters in Constant:p

Finally, look at #x, you know, he's giving X double quotes.

char* str = ToString (123132); it became str= "123132";

# # connectors and # Characters

# #连接符号由两个井号组成, its function is to join two substrings (token) in a macro definition with parameters to form a new substring. But it cannot be the first or last substring. The so-called substring (token) refers to the smallest syntactic unit that the compiler can recognize. Specific definitions in the compiler principle of a detailed explanation, but do not know it doesn't matter. It is also worth noting that the # symbol replaces the passed arguments as strings. Now let's see how they work. This is an example on MSDN.

Suppose a macro with a parameter is already defined in the program:

#define PASTER (N) printf ("token" #n "=%d", token# #n)

It also defines an reshaping variable:

int token9 = 9;

Now call the macro in the main program in the following way:

Paster (9);

At compile time, the above sentence is extended to:

printf ("token" "9" "=%d", token9);

Note that in this example, the "9" in Paster (9) is treated as a string, connected to "token" and thus token9. And #n was replaced by "9".

It is conceivable that the above program to run the result is printed on the screen token9=9


#define Display (name) printf ("#name")

int main () {

Display (name);



The particularity is that it is a macro, which handles the # number as LS says!

After processing is an additional string!

But printf ("#name");


#define Display (name) printf ("#name")

The definition strings the name,

The result is actually printf ("name")

(Take off the empty string before and after)

So the output comes naturally to name

From a different point of view,


Participate in the operation, the natural will not output ...

In addition, there are:

#define A (x) t_# #x

#define B (x) #@x

#define C (x) #x

We assume: x=1, then there are:

A (1)------) t_1

B (1)------) ' 1 '

C (1)------) "1"

Turn from: http://www.bianceng.cn


English name Stringizing Operator

I don't know the Chinese name.

In short, it means using parameters to be wrapped in double quotes

#@ is to enclose the parameters in single quotes

# #是粘结

It's too hard to read the birds on MSDN.

I'll give you an example.

After you've defined it like this,

#define PRINT (x) printf (#x)

Write in the program

Print (123);

is actually the equivalent

printf ("123");

Direct Output 123


Print ("123");

is equivalent

printf ("\" 123\ "");

The output is "123"

Just to add


Print ("\ n");

is equivalent

printf ("\ \\n");

On the output

"\ n"

printf's #. # # and # #号的使用 in C language

#define Display (name) printf ("#name")

int main () {

Display (name);


The run result is name, why not "#name"?



printf ("#name") is equivalent to

printf ("" "Name" "")


The number-sign or "stringizing" operator (#) converts macro parameters (after expansion) to string constants


printf ("#name") <1>

Equivalent to printf ("" "Name" "") <2>

And the 2nd, 3 "intermediate space-time lattice" in <2> is equivalent to ("Empty +name+ Empty")


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: 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.