Predefined macros commonly used in ansi c and Microsoft C ++)

Source: Internet
Author: User
Tags microsoft c

_ File _ the source file name is XXX. cpp.
_ Line _ the first line of code in the source file
_ Date _ source file completion date, for example, EEC 11 2012
_ Time _ source file completion time, for example, 21:35:35
_ Timestamp _ source file completion date and time, for example, Tue Dec 11 21:35:35 2012


For example, if a version of the memory allocation function malloc () is used, the above macro is used.

# Define malloc (s) _ malloc_dbg (S, _ normal_block, _ file __, _ line __)

In addition, you can use _ file _ and _ line _ in the program to determine which file has an error:

Char * pszfilename = (char *) malloc (max_path * sizeof (char ));

If (pszfilename = NULL)

Printf ("error in % S % d \ n", _ file __, _ line __);

Of course, in the actual program, _ file _ and _ line _ are used to quickly locate errors and will not be output directly, instead, you can use another program to send this information to developers by email.

 

The _ file _ type can be easily converted to the wchar_t type. msdn has this example:

# Define widen2 (x) L # x

# Define widen (x) widen2 (X)

# DEFINE _ wfile _ widen (_ file __)

Wchar_t * pwsz = _ wfile __;


# In a macro is used to stringizing the macro parameters following it. Simply put, a double quotation mark is added to the left and right sides of the macro variables it references.

For example, after # define string (x) # X is defined, the following two statements are equivalent.

Char * pchar = "hello ";

Char * pchar = string (Hello );

Another # @ is a single quotation mark (charizing operator)

# Define makechar (x) # @ x

Char CH = makechar (B); equivalent to Char CH = 'B.

 

Note that if you encounter # Or # In a macro, the nested Macro will no longer be expanded. What does that mean? For example, char * pchar = string (_ file _) is used. Although _ file _ is a macro, the compiler does not expand it, therefore, pchar will point to "_ file _" instead of "D: \ XXX. the name of the CPP source file. Therefore, you need to add an intermediate conversion macro, first parse _ file _ into a "D: \ XXX. cpp" string.

Two macros are defined as follows:

# DEFINE _ string (x) # x

# Define string (x) _ string (X)

Then, call the following statement to output the path of the source file "".

Char * pchar = string (_ file __);

Printf ("% S % s \ n", pchar, _ file __);

We can compare the differences between string (_ file _) and _ file _. The front will contain double quotation marks, and the last one will not contain double quotation marks.

 

Next, let's talk about the # function. It can be spliced with a token-pasting operator ).

There is an example on msdn:

# Define Paster (n) printf ("token" # N "= % d \ n", Token # N)

Int token9= 100;

Call Paster (9) Again. After the macro is expanded, the token # N is directly merged into token9. The entire statement becomes

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

In the C language, the two connected double quotation marks in the string will be automatically ignored, so the above sentence is equivalent

Printf ("token9 = % d", token9 );.

That is, the output token9 = 100

 

 

With the above basics, let's look at Example 1.

# Define widen2 (x) L # x

# Define widen (x) widen2 (X)

# DEFINE _ wfile _ widen (_ file __)

Wchar_t * pwsz = _ wfile __;

The L in the first macro converts an ANSI string to a unicode string. For example, wchar_t * pstr = l "hello ";

Let's take a look at wchar_t * pwsz = _ wfile __;

_ Wfile _ is first expanded to widen (_ file _), and then expanded to widen2 ("_ file _ string "), concatenate the string in the format of L "_ file _", that is, l "d: \ XXX. CPP "to obtain the Unicode string and assign the string address to the pwsz pointer.

 

In VC, _ T () and text () are also used.

In the tchar. h header file, you can find:

# DEFINE _ T (x) _ T (X)

# DEFINE _ T (x) L # x

In the WINNT. h header file, you can find

# Define text (quote) _ text (quote) // r_winnt

# DEFINE _ text (quote) L # quote // r_winnt

Therefore, it is not difficult to understand why the third statement has an error: Error c2065: 'lsztext': Undeclared identifier

Wprintf (text ("% S % s \ n"), _ T ("hello"), text ("hello "));

Char sztext [] = "hello ";

Wprintf (text ("% S % s \ n"), _ T (sztext), text (sztext ));

After "hello" is defined as a macro, it can run correctly.

# Define sztext "hello"

Wprintf (text ("% S % s \ n"), _ T (sztext), text (sztext ));

Note: Because vc6.0 uses ANSI encoding by default, you must first set it to unicode encoding, select setting from the project menu, and then select Preprocessor in the C/C ++ label dialog box. In the Preprocessor definitions edit box, remove _ MBCS and add _ Unicode and Unicode.

 

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.