A conditional in the c preprocessor resembles in some

Info icon This preview shows pages 43–45. Sign up to view the full content.

View Full Document Right Arrow Icon
A conditional in the C preprocessor resembles in some ways an if statement in C, but it is important to understand the difference between them. The condition in an if statement is tested during the execution of your program. Its purpose is to allow your program to behave differently from run to run, depending on the data it is operating on. The condition in a preprocessing conditional directive is tested when your program is compiled. Its purpose is to allow different code to be included in the program depending on the situation at the time of compilation. However, the distinction is becoming less clear. Modern compilers often do test if statements when a program is compiled, if their conditions are known not to vary at run time, and eliminate code which can never be executed. If you can count on your compiler to do this, you may find that your program is more readable if you use if statements with constant conditions (perhaps determined by macros). Of course, you can only use this to exclude code, not type definitions or other preprocessing directives, and you can only do it if the code remains syntactically valid when it is not to be used. GCC version 3 eliminates this kind of never-executed code even when not optimizing. Older versions did it only when optimizing. 4.1 Conditional Uses There are three general reasons to use a conditional. A program may need to use different code depending on the machine or operating system it is to run on. In some cases the code for one operating system may be erroneous on another operating system; for example, it might refer to data types or constants that do not exist on the other system. When this happens, it is not enough to avoid executing the invalid code. Its mere presence will cause the compiler to reject the program. With a preprocessing conditional, the offending code can be effectively excised from the program when it is not valid. You may want to be able to compile the same source file into two different programs. One version might make frequent time-consuming consistency checks on its intermedi- ate data, or print the values of those data for debugging, and the other not.
Image of page 43

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

View Full Document Right Arrow Icon
Chapter 4: Conditionals 40 A conditional whose condition is always false is one way to exclude code from the program but keep it as a sort of comment for future reference. Simple programs that do not need system-specific logic or complex debugging hooks generally will not need to use preprocessing conditionals. 4.2 Conditional Syntax A conditional in the C preprocessor begins with a conditional directive : ‘ #if ’, ‘ #ifdef ’ or #ifndef ’. 4.2.1 Ifdef The simplest sort of conditional is #ifdef MACRO controlled text #endif /* MACRO */ This block is called a conditional group . controlled text will be included in the output of the preprocessor if and only if MACRO is defined. We say that the conditional succeeds if MACRO is defined, fails if it is not.
Image of page 44
Image of page 45
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