Macros in C Language 5

Source: Internet
Author: User

The original Article is here

Character-based

Sometimes you want to convert a macro parameter into a character constant. You want to insert a macro parameter into a string and expect it to be simply replaced with a character constant. The idea is good, but it is impossible. However, you can use the "#" preprocessing operator. When a macro parameter is prefixed with "#", The Preprocessor replaces the macro parameter symbol with the character constant that is the same as the macro parameter symbol. Unlike conventional parameter replacement, this type of conversion is not called macro expansion, but "charistic ".

If you mix a macro parameter into a string and then encode it, this operation is impossible. Instead, you should write a series of independent character constants and place macro parameters between them. The Preprocessor characters macro parameters, and then the C compiler combines all independent strings into a long string.

The following is an example of a characters' macro.

# Define warn_if (exp) \ do {If (exp) \ fprintf (stderr, "Warning:" # exp "\ n") ;}\ while (0) warn_if (x = 0) ;=> do {If (x = 0) fprintf (stderr, "Warning: "" X = 0 "" \ n ");} while (0 );
 
The exp parameter is used in the IF statement at one time, and is also converted into characters in the fprintf function at another time. If X is also a macro, it will continue to be expanded in the IF statement, but it will not be converted into characters.
 
Do While (0) loop is a small trick, making it possible to write warn_if (ARG); otherwise, the IF statement in the macro content will put the normalC LanguageBad process. See brackets for alignment.
The character conversion in C is not as simple as adding quotation marks on both sides of the parameter. It also escapes various special characters to obtain a valid C string. Therefore, the string P = "foo \ n" is used as a parameter to input a macro."P = \" foo \ n \";". However, the characters that are special characters are retained as they are, for example, the characters '\ n' are converted into "\ n ".
 
All leading and trailing spaces are ignored. All spaces in the middle will be converted into a space. The annotation has been killed by the pre-processor for 1 million years before the execution of the characters' conversion, so there will be no annotation in the results of the conversion.
 
It is impossible to convert a macro parameter to a character constant instead of a String constant.
 
If you want to see what a macro looks like after it is expanded, you can use two macros:
# Define xstr (s) STR (s) # define STR (s) # s # define Foo 4 STR (FOO) => "foo" xstr (FOO) ==> xstr (4) ==> STR (4) ==> "4"

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.