Stringizing Operator (#)

xiaoxiao2021-03-30  201

There is a function in Assert.h_ssert__cdecl _assert (void *, void *, unsigned);

There are also this #define assert (void) ((exp) || (_ASSERT (#exp, __file__, __line__), 0))

So what do you mean by "#" inside #exp? Operator? -------------------------------------------------- -------------------------

Convert to strings such as #define Turn (Exp) #exp

You can write such syntax Printf ("% s", TURN (TEST)) in the program; output result is: Test ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------- -

The Following Example Shows A Macro Definition That Includes The Stringizing Operator And a Main Function That Invokes The Macro:

#define stringer (x) Printf (#x "/ n")

Void main () {STRINGER (in quotes in the printf function call / n); Stringer ("in quotes when protocated to the screen" / n); stringer ("this: /" prints an escaped double quote ");}

Such Invocations Would Be Expanded During Preprocessing, Producing The Following Code:

Void main () {Printf ("in quotes in the printf function call / n" "/ n"); Printf ("/" / "/ n" / n "); Printf (" / "This: ///" Prints an escaped double quote / "" / n ");}

When the Program Is Run, Screen Output for Each Line IS FOLLOWS:

In Quotes in the Printf Function Call

"In quotes when printed to the screen"

"This: /" prints an escaped double quotation mark "

-------------------------------------------------- ---------------------------

### The # and ## Operators Are used with the #define macro. Using # causes the first argument after the # to be returned as a string in quotes. Using ## Concatenates What's Before The ## with what's after.

Example Code: for example: for example # define to_string (s) # s

Will Make the Compiler Turn this Command

Cout << to_String (Hello World!) << Endl;

INTO

COUT << "Hello World!" << endl;

Here is an example of the ## Command:

#define concatenate (x, y) x ## y ... int xy = 10; ...

This Code Will Make The Compiler Turn

COUT << Concatenate (x, y) << endl;

INTO

Cout << xy << endl;

Which Will, Of Course, Display '10' To Standard Output.

转载请注明原文地址:https://www.9cbs.com/read-130691.html

New Post(0)