The : an example program exercising it’s main features main() /************************************************************* Whether or not you provide preprocessor commands, the preprocessor does a few things for you automatically: 1. by default it replaces all C comments by single spaces 2. all backslash- sequences are deleted, which is the mechanism for continuing lines; e.g., the 3 source lines one\ two \ three are converted to the single line onetwo three by the preprocessor 3. predefined names are replaced with their expansions; these are simple built-in macros whose meanings are always the same; their names all start and end with a double underscore; a few of these are __FILE__ returns name of current module as a C string constant (#include changes __FILE__ to the include name) __LINE__ returns number of the text file line number __LINE__ occurs on as a C integer constant (#include changes __LINE__ to the include file it can be adjusted further if the application needs to use #line) __DATE__ returns an 11 character string with the current date as a C string constant __TIME__ returns an 8 character string with the current time as a C string constant as a C string constant in each case

You can run the preprocessor by itself by entering cpp | less or cc –E | less at the prompt (usually cc produces cleaner output). **************************************************************/

{ /***************** some working variables ********************/ int x,y,z; struct {int indx; char name[10]} test; /*************************************************************/ printf ("\nEX1 ------chopped up printf\n"); /************************************************************* a very chopped up 'printf("START\n");' **************************************************************/ p\ r\ i\ ntf("STA\ RT\\ n"); printf ("\nEX2 ------predefined macros\n"); /************************************************************* predefined macros **************************************************************/ printf("File: %s\nLine: %\nDate: %s\nTime: %s\n", __FILE__, __LINE__, __DATE__, __TIME__);

printf ("\nEX3 ------simple replacement macro\n"); /************************************************************* simple replacement macro: replace the symbol ONE by the 7 characters "one\n" where it occurs subsequently in this source module; the convention is to name macros using all caps **************************************************************/ #define ONE "one\n" printf(ONE); printf ("\nEX4 ------macro redefine omitting #undef\n"); /************************************************************* redefine the macro: produces a warning, because this might be a programmer error, but it does it anyway; the new definition is in effect for the rest of the module **************************************************************/ #define ONE "two\n" printf(ONE); printf ("\nEX5 ------macro redefine using #undef\n"); /************************************************************* undefine the macro: undefining a macro before redefining it essentially tells the preprocessor this is an intended action, so no warning occurs **************************************************************/ #undef ONE #define ONE "three\n" printf(ONE); printf ("\nEX6 ------check definition with #ifdef\n"); /************************************************************* check to see if the macro is defined **************************************************************/ #ifdef ONE printf("if part 1\n"); #else printf("else part 1\n"); #endif printf ("\nEX7 ------check definition with #ifndef\n"); /************************************************************* check to see if the macro is not defined **************************************************************/ #ifndef ONE printf("if part 2\n"); #else printf("else part 2\n"); #endif printf ("\nEX8 ------use of general #if to reset SYMBSIZE\n"); /************************************************************* there is a general if construction **************************************************************/ #define SYMBSIZE 10 printf("SYMBSIZE = %d\n",SYMBSIZE); #if SYMBSIZE == 10 #undef SYMBSIZE #define SYMBSIZE 20 #endif printf("SYMBSIZE = %d\n",SYMBSIZE); printf ("\nEX9 ------macro with arguments (SWAP)\n"); /************************************************************* a macro can have arguments **************************************************************/ #define SWAP(A,B,Temp) {\ Temp = A; \ A = B; \ B = Temp;} x=1; y=2; printf("swapping x=%d y=%d\n",x,y); SWAP(x,y,z) printf("result: x=%d y=%d\n",x,y); printf ("\nEX10 ------concatenation in macros using ##\n"); /************************************************************* macro arguments can be concatenated using ## all blanks surrounding ## are deleted **************************************************************/ #define STRUCTURE test #define WITH(Name) STRUCTURE. ## Name test.indx = 1; strcpy(test.name,"test name"); printf("name extracted for EX10: %s\n",WITH(name)); printf ("\n---#include tests---\n"); check(); } check() { /************************************************************* system include files (header files) provide the definitions needed to access operating system routines and module libraries #include **************************************************************/ printf ("\nEX11 ------simple include of user file\n"); /************************************************************* personal include files are for items (macro definitions or otherwise) you want to use across multiple modules, usually for consistency **************************************************************/ #include "demo1.h" printf ("\nEX12 ------simple include via macro name\n"); /************************************************************* the object of a #include can be a macro name **************************************************************/ #define MYHEADER "demo2.h" #include MYHEADER File contents listed on next page printf ("\nEX13 ------include within an include file\n"); /************************************************************* an include file can issue a #include **************************************************************/ #include "demo3w4.h" } Contents of “demo1.h” printf("demo 1 include\n");

Contents of “demo2.h” printf("demo 2 include\n");

Contents of “demo3w4.h” printf("demo3w4.h including demo4\n"); #include "demo4.h"

Contents of “demo4.h” printf("demo 4 include\n");