In the present implementation a single use of import

Info icon This preview shows pages 15–17. Sign up to view the full content.

View Full Document Right Arrow Icon
In the present implementation, a single use of ‘ #import ’ will prevent the file from ever being read again, by either ‘ #import ’ or ‘ #include ’. You should not rely on this; do not use both ‘ #import ’ and ‘ #include ’ to refer to the same header file. Another way to prevent a header file from being included more than once is with the #pragma once ’ directive. If ‘ #pragma once ’ is seen when scanning a header file, that file will never be read again, no matter what. #pragma once ’ does not have the problems that ‘ #import ’ does, but it is not recognized by all preprocessors, so you cannot rely on it in a portable program. 2.6 Computed Includes Sometimes it is necessary to select one of several different header files to be included into your program. They might specify configuration parameters to be used on different sorts of operating systems, for instance. You could do this with a series of conditionals, #if SYSTEM_1 # include "system_1.h" #elif SYSTEM_2 # include "system_2.h" #elif SYSTEM_3 ... #endif That rapidly becomes tedious. Instead, the preprocessor offers the ability to use a macro for the header name. This is called a computed include . Instead of writing a header name as the direct argument of ‘ #include ’, you simply put a macro name there instead: #define SYSTEM_H "system_1.h" ... #include SYSTEM_H SYSTEM_H will be expanded, and the preprocessor will look for ‘ system_1.h ’ as if the #include ’ had been written that way originally. SYSTEM_H could be defined by your Make- file with a ‘ -D ’ option. You must be careful when you define the macro. #define ’ saves tokens, not text. The preprocessor has no way of knowing that the macro will be used as the argument of #include ’, so it generates ordinary tokens, not a header name. This is unlikely to cause problems if you use double-quote includes, which are close enough to string constants. If you use angle brackets, however, you may have trouble. The syntax of a computed include is actually a bit more general than the above. If the first non-whitespace character after ‘ #include ’ is not ‘ " ’ or ‘ < ’, then the entire line is macro-expanded like running text would be. If the line expands to a single string constant, the contents of that string constant are the file to be included. CPP does not re-examine the string for embedded quotes, but neither does it process backslash escapes in the string. Therefore
Image of page 15

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

View Full Document Right Arrow Icon
Chapter 2: Header Files 12 #define HEADER "a\"b" #include HEADER looks for a file named ‘ a\"b ’. CPP searches for the file according to the rules for double- quoted includes. If the line expands to a token stream beginning with a ‘ < ’ token and including a ‘ > token, then the tokens between the ‘ < ’ and the first ‘ > ’ are combined to form the filename to be included. Any whitespace between tokens is reduced to a single space; then any space after the initial ‘ < ’ is retained, but a trailing space before the closing ‘ > ’ is ignored. CPP searches for the file according to the rules for angle-bracket includes.
Image of page 16
Image of page 17
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