If the defined operator appears as a result of a

Info icon This preview shows pages 46–48. Sign up to view the full content.

If the defined operator appears as a result of a macro expansion, the C standard says the behavior is undefined. GNU cpp treats it as a genuine defined operator and evaluates it normally. It will warn wherever your code uses this feature if you use the command-line option ‘ -pedantic ’, since other compilers may handle it differently. 4.2.4 Else The ‘ #else ’ directive can be added to a conditional to provide alternative text to be used if the condition fails. This is what it looks like: #if expression text-if-true #else /* Not expression */ text-if-false #endif /* Not expression */ If expression is nonzero, the text-if-true is included and the text-if-false is skipped. If expression is zero, the opposite happens. You can use ‘ #else ’ with ‘ #ifdef ’ and ‘ #ifndef ’, too. 4.2.5 Elif One common case of nested conditionals is used to check for more than two possible alter- natives. For example, you might have #if X == 1 ... #else /* X != 1 */ #if X == 2 ... #else /* X != 2 */ ... #endif /* X != 2 */ #endif /* X != 1 */ Another conditional directive, ‘ #elif ’, allows this to be abbreviated as follows: #if X == 1 ... #elif X == 2 ... #else /* X != 2 and X != 1*/
Image of page 46

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

Chapter 5: Diagnostics 43 ... #endif /* X != 2 and X != 1*/ #elif ’ stands for “else if”. Like ‘ #else ’, it goes in the middle of a conditional group and subdivides it; it does not require a matching ‘ #endif ’ of its own. Like ‘ #if ’, the ‘ #elif directive includes an expression to be tested. The text following the ‘ #elif ’ is processed only if the original ‘ #if ’-condition failed and the ‘ #elif ’ condition succeeds. More than one ‘ #elif ’ can go in the same conditional group. Then the text after each #elif ’ is processed only if the ‘ #elif ’ condition succeeds after the original ‘ #if ’ and all previous ‘ #elif ’ directives within it have failed. #else ’ is allowed after any number of ‘ #elif ’ directives, but ‘ #elif ’ may not follow #else ’. 4.3 Deleted Code If you replace or delete a part of the program but want to keep the old code around for future reference, you often cannot simply comment it out. Block comments do not nest, so the first comment inside the old code will end the commenting-out. The probable result is a flood of syntax errors. One way to avoid this problem is to use an always-false conditional instead. For instance, put #if 0 before the deleted code and #endif after it. This works even if the code being turned off contains conditionals, but they must be entire conditionals (balanced ‘ #if ’ and #endif ’). Some people use #ifdef notdef instead. This is risky, because notdef might be acci- dentally defined as a macro, and then the conditional would succeed. #if 0 can be counted on to fail. Do not use #if 0 for comments which are not C code. Use a real comment, instead. The interior of #if 0 must consist of complete tokens; in particular, single-quote characters must balance. Comments often contain unbalanced single-quote characters (known in English as apostrophes). These confuse #if 0 . They don’t confuse ‘ /* ’.
Image of page 47
Image of page 48
This is the end of the preview. Sign up to access the rest of the document.
  • Summer '13
  • Noone
  • GCC, C preprocessor

{[ 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