The first example uses a manifest constant to represent a "magic number." Suppose your application is doing metric conversions. Instead of sprinkling the conversion factor around in your code, which would just be a cryptic number, you can #define it as a manifest constant, which eliminates the possibility that you might get the number wrong in some places and makes your code self-documenting:

#define LB_PER_KG 2.2046 // Number of pounds per kilogram

...

nPounds = form.kg.value * LB_PER_KG

The second example uses a manifest constant to represent a simple constant in your application. Suppose you’re testing several different techniques to see which one accomplishes the same task the fastest. You need to repeat the task many times to get measurable results, so you use a manifest constant to represent the number of times you want each test to be run. By using a single manifest constant, you can easily change the number of times each test is run and calculate the average time:

#define NUM_REPS 10000 // Number of times to repeat each test

...

for n = 1 to NUM_REPS

   // Test 1 

endfor

for n = 1 to NUM_REPS

   // Test 2 

endfor

...

? "Average time for test 1", time[1] / NUM_REPS

The following example uses a manifest constant for a file name that is used in different parts of an application:

#define QWK_FILE "IMF.QWK"

#define MESSAGE_FILE "MESSAGES.DAT"

...

fMsg = new File( )

fMsg.create( MESSAGE_FILE )

...

z = new ZipFile( QWK_FILE ) // Create compressed file

z.store( MESSAGE_FILE ) // Store the message file

...

class ZipFile( cFileName ) of File

   // Code to implement ZipFile class 

endclass

This example demonstrates conditional compilation. Two preprocessor identifiers are used: a DEBUG flag, and a BUILD number:

#define DEBUG // Comment out if not debug version

#define BUILD 35 // Current build number

...

#if BUILD < 20

   // Older code 

#else

   // Current code 

   #ifdef DEBUG 

      // Include DEBUG code 

   #endif 

#endif