The do and while 0 are a kludge to make it possible

Info icon This preview shows pages 21–23. Sign up to view the full content.

View Full Document Right Arrow Icon
The do and while (0) are a kludge to make it possible to write WARN_IF ( arg ); , which the resemblance of WARN_IF to a function would make C programmers want to do; see Section 3.10.3 [Swallowing the Semicolon], page 35 . Stringification in C involves more than putting double-quote characters around the frag- ment. The preprocessor backslash-escapes the quotes surrounding embedded string con- stants, and all backslashes within string and character constants, in order to get a valid C string constant with the proper contents. Thus, stringifying p = "foo\n"; results in "p = \"foo\\n\";" . However, backslashes that are not inside string or character constants are not duplicated: ‘ \n ’ by itself stringifies to "\n" .
Image of page 21

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter 3: Macros 18 All leading and trailing whitespace in text being stringified is ignored. Any sequence of whitespace in the middle of the text is converted to a single space in the stringified result. Comments are replaced by whitespace long before stringification happens, so they never appear in stringified text. There is no way to convert a macro argument into a character constant. If you want to stringify the result of expansion of a macro argument, you have to use two levels of macros. #define xstr(s) str(s) #define str(s) #s #define foo 4 str (foo) 7→ "foo" xstr (foo) 7→ xstr (4) 7→ str (4) 7→ "4" s is stringified when it is used in str , so it is not macro-expanded first. But s is an ordinary argument to xstr , so it is completely macro-expanded before xstr itself is expanded (see Section 3.10.6 [Argument Prescan], page 37 ). Therefore, by the time str gets to its argument, it has already been macro-expanded. 3.5 Concatenation It is often useful to merge two tokens into one while expanding macros. This is called token pasting or token concatenation . The ‘ ## ’ preprocessing operator performs token pasting. When a macro is expanded, the two tokens on either side of each ‘ ## ’ operator are combined into a single token, which then replaces the ‘ ## ’ and the two original tokens in the macro expansion. Usually both will be identifiers, or one will be an identifier and the other a preprocessing number. When pasted, they make a longer identifier. This isn’t the only valid case. It is also possible to concatenate two numbers (or a number and a name, such as 1.5 and e3 ) into a number. Also, multi-character operators such as += can be formed by token pasting. However, two tokens that don’t together form a valid token cannot be pasted together. For example, you cannot concatenate x with + in either order. If you try, the preprocessor issues a warning and emits the two tokens. Whether it puts white space between the tokens is undefined. It is common to find unnecessary uses of ‘ ## ’ in complex macros. If you get this warning, it is likely that you can simply remove the ‘ ## ’.
Image of page 22
Image of page 23
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern