How are portions of a program disabled in demo versions?


If you are distributing a demo version of your program, the preprocessor can be used to enable or disable portions of your program. The following portion of code shows how this task is accomplished, using the preprocessor directives #if and #endif:

int save_document(char* doc_name)
     printf("Sorry! You can't save documents using the DEMO version of
             this program!\n");

When you are compiling the demo version of your program, insert the line #define DEMO_VERSION and the preprocessor will include the conditional code that you specified in the save_document() function. This action prevents the users of your demo program from saving their documents.

As a better alternative, you could define DEMO_VERSION in your compiler options when compiling and avoid having to change the source code for the program.

This technique can be applied to many different situations. For instance, you might be writing a program that will support several operating systems or operating environments. You can create macros such asWINDOWS_VERUNIX_VER, and DOS_VER that direct the preprocessor as to what code to include in your program depending on what operating system you are compiling for.


© 2017