Docsity

Understanding Preprocessor Directives in Windows Programming: #define, #include, #if, and , Slides of Windows Programming

Partial preview of the text

Download Understanding Preprocessor Directives in Windows Programming: #define, #include, #if, and and more Slides Windows Programming in PDF only on Docsity!

Windows Programming

Lecture 05

Preprocessor

Preprocessor Directives

Preprocessor directives are instructions for

compiler.

Examples of Preprocessor

Directives

#define #error #undef #include

#if #else #elif #endif

#ifdef #line #ifndef #pragma

#define ARRAY_SIZE 10
Before Preprocessing
char array[ARRAY_SIZE];
After Preprocessing
char array[10];

identifier is not replaced if it appears

  • in a comment
  • within a string, or
  • as part of a longer identifier

#define directive

#define WIDTH 10

#define HEIGHT WIDTH+

HEIGHT is replaced with^ WIDTH+25 is replaced with 10+

#define directive

identifier remains defined and can be tested
  • It can be checked using #if and #ifdef
directives.
  • #if defined is same as #ifdef.

#if, #elif, #else

#elif is same as #else #if

Preprocessor Operators

The defined is a preprocessor operator.

#if defined MAX

is equivalent to

#ifdef MAX

Preprocessor operator

Preprocessor Directive

#error directive

  • The directive #error causes the preprocessor to report a fatal error. The rest of the line that follows #error is used as the error message. The line must consist of complete tokens.
  • You would use #error inside of a conditional statment

that detects a combination of parameters which you know the program does not properly support.

#error directive

#if !defined(__cplusplus)
#error Must use C++ language
#endif

#undef Directive

Once an identifier has been undefined, that
identifier may be redefined by a subsequent
#define directive. The new definition need not
have any resemblance to the old definition.

#undef Directive

#define SPEED ………… some code here…………… … … … … … … … #undef SPEED ifndef(SPEED) #error Speed not defined #endif