Ch. 6: Preprocessing Directives

Ch. 6: Preprocessing Directives - Preprocessing Directives...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Preprocessing Directives Outline Introduction Source File Inclusion Symbolic Constants and Macros Conditional Compilation NULL Directive Predefined Macros Sample Problem Converting Token to Strings Token Merging in Macro Expansions Line Control Error Directive Pragma Directive Predefined Macros in Ch
Background image of page 1

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

View Full DocumentRight Arrow Icon
Introduction Preprocessing – Affect program preprocessing and execution – Capabilities • Inclusion of additional C source files • Definition of symbolic constants and macros • Conditional preprocessing of a program Format of preprocessing directives A preprocessing directive consists of a sequence of preprocessing tokens that begins with a pound sign #, which must be the first non-space character on the line. Some preprocessing directives are listed below.
Background image of page 2
Directive Description #define Define a preprocessor macro. #elif Alternatively include some text based on the value of another expression, if the previous #if , #ifdef , #ifndef , or #leif test failed. #else Alternatively include some text, if the previous #if , #ifdef , #ifndef , or #elif test failed. #endif Terminate conditional text. #error Produce a compile-time error with a designated message. #if Conditionally include text, based on the value of an expression. #ifdef Conditionally include text, based on whether a macro name is defined. #ifndef Conditionally include text, based on if a name is not a defined macro. #include Insert text from another source file. #line Give a line number for message. #pragma Compiler/interpreter specific features, not in C standard # Null directive defined Preprocessing operator that yields 1 if a name is defined as a preprocessing macro and 0 otherwies; used in #if and #elif .
Background image of page 3

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

View Full DocumentRight Arrow Icon
Conditional Compilation Conditional compilation – Enables the user to control the compilation of the program, screen out portions of source code that are not to be compiled. •S t r u c t u r e – The structure is similar to if and else statement in C. Conditional preprocessing directives
Background image of page 4
Preprocessing directives of the forms #if expr1 /* . .. */ #elif expr2 /* . .. */ #else /* . .. */ #endif check whether the controlling expression evaluates to nonzero. Every #if ends with #endif Example: #if defined(_HPUX_) printf(“I am using HP-UX\n”); #elif defined(_WIN32_) printf(“I am using Windows\n); #endif
Background image of page 5

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

View Full DocumentRight Arrow Icon
Preprocessing directives of the forms # ifdef identifier # ifndef identifier check whether the identifier is or is not currently defined as a macro name. #ifdef identifier is the short form of #if defined( identifier ) #ifndef identifier is the short form of #if !defined( identifier ) Each directive’s condition is checked in order. If it evaluates to false (zero), then the group that it controls is skipped: directives are processed only through the name that determines the directive in order to keep track of the level of nested conditionals. Only the first group whose control condition evaluates to true (nonzero) is processed.
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/21/2008 for the course EME 005 taught by Professor Cheng during the Fall '07 term at UC Davis.

Page1 / 26

Ch. 6: Preprocessing Directives - Preprocessing Directives...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online