The controlled text inside of a conditional can

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

View Full Document Right Arrow Icon
The controlled text inside of a conditional can include preprocessing directives. They are executed only if the conditional succeeds. You can nest conditional groups inside other conditional groups, but they must be completely nested. In other words, ‘ #endif ’ always matches the nearest ‘ #ifdef ’ (or ‘ #ifndef ’, or ‘ #if ’). Also, you cannot start a conditional group in one file and end it in another. Even if a conditional fails, the controlled text inside it is still run through initial trans- formations and tokenization. Therefore, it must all be lexically valid C. Normally the only way this matters is that all comments and string literals inside a failing conditional group must still be properly ended. The comment following the ‘ #endif ’ is not required, but it is a good practice if there is a lot of controlled text , because it helps people match the ‘ #endif ’ to the corresponding #ifdef ’. Older programs sometimes put MACRO directly after the ‘ #endif ’ without enclosing it in a comment. This is invalid code according to the C standard. CPP accepts it with a warning. It never affects which ‘ #ifndef ’ the ‘ #endif ’ matches. Sometimes you wish to use some code if a macro is not defined. You can do this by writing ‘ #ifndef ’ instead of ‘ #ifdef ’. One common use of ‘ #ifndef ’ is to include code only the first time a header file is included. See Section 2.4 [Once-Only Headers], page 10 . Macro definitions can vary between compilations for several reasons. Here are some samples. Some macros are predefined on each kind of machine (see Section 3.7.3 [System-specific Predefined Macros], page 31 ). This allows you to provide code specially tuned for a particular machine. System header files define more macros, associated with the features they implement. You can test these macros with conditionals to avoid using a system feature on a machine where it is not implemented.
Image of page 44

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

View Full Document Right Arrow Icon
Chapter 4: Conditionals 41 Macros can be defined or undefined with the ‘ -D ’ and ‘ -U ’ command line options when you compile the program. You can arrange to compile the same source file into two different programs by choosing a macro name to specify which program you want, writing conditionals to test whether or how this macro is defined, and then controlling the state of the macro with command line options, perhaps set in the Makefile. See Chapter 12 [Invocation], page 56 . Your program might have a special header file (often called ‘ config.h ’) that is adjusted when the program is compiled. It can define or not define macros depending on the features of the system and the desired capabilities of the program. The adjustment can be automated by a tool such as autoconf , or done by hand.
Image of page 45
Image of page 46
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