Display the macro command text tips defined in CC ++

xiaoxiao2021-03-05  24

This tip is very useful in the C / C programming process! I hope to help all GGDDMMJJ. Here is the implementation method:

In fact, it is very simple, usually we love to fill in some macro definitions in your own CPP code, such as below:

#define mymacro_filetype_error 0x0000001

#define mymacro_closefile_error 0x0000002

......

Then apply these MACRO in the subsequent code, it is very convenient, right? : D

If these definitions are certain error code (or other 啥), we usually display the meaning of their representatives in the screen or log. When reading these macro commands, it is not difficult to find that they are defined very readable, and the average person can know what they mean by them. How to display these macros directly on the log or screen to reduce the trouble of writing our writing?

Workaround, please see:

#define error_text_ (e) #E // <--- See here! That's so easy, right?: p

#define mymacro_filetype_error 0x0000001

#define mymacro_closefile_error 0x0000002

......

Then we can write this in the code:

Cout << Error_Text_ (MyMacro_filety_ERROR) << ENDL;

After execution, the screen will directly display the words "mymacro_filety_error", cool :)

You can also transform this macro of Error_Text_ (E), making it more practical!

For example, it is defined as this:

#define ReturnText_ (e) case e: return # e # define MyMacro_FileType_Error 0x000001 # define MyMacro_CloseFile_Error 0x000002 # define MSGS () / ReturnText_ (maHelloWorld1); / ReturnText_ (maHelloWorld2);

At this point you can define a function to display the content text content.

String geteRortext (int NerrorCode)

{

Switch (NerrorCode)

{

Msgs ();

/ / You can continue to add additional information processing.

} // End Switch

}

.....

Call getErrorText () directly in your code,

Cout << getErrorText (mymacro_filetype_error) .c_str () << Endl;

The following is a reference to MSDN in Chinese:

Index keyword = # (Stringizing Operator)

Stringizing Operator (#)

The number-sign or "stringizing" operator (#) converts macro parameters (after expansion) to string constants. It is used only with macros that take arguments. If it precedes a formal parameter in the macro definition, the actual argument passed by the macro invocation is enclosed in quotation marks and treated as a string literal. The string literal then replaces each occurrence of a combination of the stringizing operator and formal parameter within the macro definition.White space preceding the first token of the actual argument and following the last token of the actual argument is ignored. Any white space between the tokens in the actual argument is reduced to a single white space in the resulting string literal. Thus, if a comment occurs between two tokens in the actual argument, it is reduced to a .

Further, if a character contained in the argument usually requires an escape sequence when used in a string literal (for example, the quotation mark ( ") or backslash (/) character), the necessary escape backslash is automatically inserted before the character. 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 printed to the screen" / n);

STRINGER ("this: /" print ");

}

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

void main ()

{

Printf ("in quotes in the sharpf function call / n" "/ n"); Printf ("/" in quotes when printed to the screen / "/ 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 "

Microsoft Specific

The Microsoft C (versions 6.0 and earlier) extension to the ANSI C standard that previously expanded macro formal arguments appearing inside string literals and character constants is no longer supported. Code that relied on this extension should be rewritten using the stringizing (#) operator.

End Microsoft Specific

Or you can directly access Microsoft related URLs online:

Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/prepr_13.asp