422 if the if directive allows you to test the value

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

View Full Document Right Arrow Icon
4.2.2 If The ‘ #if ’ directive allows you to test the value of an arithmetic expression, rather than the mere existence of one macro. Its syntax is #if expression controlled text #endif /* expression */ expression is a C expression of integer type, subject to stringent restrictions. It may contain Integer constants. Character constants, which are interpreted as they would be in normal code. Arithmetic operators for addition, subtraction, multiplication, division, bitwise opera- tions, shifts, comparisons, and logical operations ( && and || ). The latter two obey the usual short-circuiting rules of standard C. Macros. All macros in the expression are expanded before actual computation of the expression’s value begins. Uses of the defined operator, which lets you check whether macros are defined in the middle of an ‘ #if ’. Identifiers that are not macros, which are all considered to be the number zero. This allows you to write #if MACRO instead of #ifdef MACRO , if you know that MACRO, when defined, will always have a nonzero value. Function-like macros used without their function call parentheses are also treated as zero. In some contexts this shortcut is undesirable. The ‘ -Wundef ’ option causes GCC to warn whenever it encounters an identifier which is not a macro in an ‘ #if ’. The preprocessor does not know anything about types in the language. Therefore, sizeof operators are not recognized in ‘ #if ’, and neither are enum constants. They will be taken as identifiers which are not macros, and replaced by zero. In the case of sizeof , this is likely to cause the expression to be invalid. The preprocessor calculates the value of expression . It carries out all calculations in the widest integer type known to the compiler; on most machines supported by GCC this is 64 bits. This is not the same rule as the compiler uses to calculate the value of a constant expression, and may give different results in some cases. If the value comes out to be nonzero, the ‘ #if ’ succeeds and the controlled text is included; otherwise it is skipped.
Image of page 45

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

View Full Document Right Arrow Icon
Chapter 4: Conditionals 42 4.2.3 Defined The special operator defined is used in ‘ #if ’ and ‘ #elif ’ expressions to test whether a certain name is defined as a macro. defined name and defined ( name ) are both expressions whose value is 1 if name is defined as a macro at the current point in the program, and 0 otherwise. Thus, #if defined MACRO is precisely equivalent to #ifdef MACRO . defined is useful when you wish to test more than one macro for existence at once. For example, #if defined (__vax__) || defined (__ns16000__) would succeed if either of the names __vax__ or __ns16000__ is defined as a macro. Conditionals written like this: #if defined BUFSIZE && BUFSIZE >= 1024 can generally be simplified to just #if BUFSIZE >= 1024 , since if BUFSIZE is not defined, it will be interpreted as having the value zero.
Image of page 46
Image of page 47
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