Preprocessor Statements #ifdef, #else, #endif

These provide a rapid way to "clip" out and insert code.

Consider;

     #define FIRST

     main()
     {
        int a, b, c;

#ifdef FIRST
       a=2; b=6; c=4;
#else
       printf("Enter a:");
       scanf("%d", &a);
     
       printf("Enter a:");
       scanf("%d", &a);

       printf("Enter a:");
       scanf("%d", &a);
#endif
       additonal code

Note that if FIRST is defined (which it is in the above) the values of a, b and c are hardcoded to values of 2, 6 and 4. This can save a lot of time when developing software as it avoids tediously typing everything in each and everytime you run your routine. When FIRST is defined, all that is passed to the compiler is the code between the #ifdef and the #else. The code between the #else and the #endif is not seen by the compiler. It is as if it were all a comment.

Once you have your routine working, and desire to insert the printf and scanfs, all that is required is to go back and delete the the #define FIRST. Now, the compiler does not see the;

     a=2; b=6; c=4;

statement, but does see the printf and scanfs.


Quite often you will have code which doesn't work, but you really don't want to throw it away. I see many students doing this;

/*
     unwanted code
*/

And this is acceptable. Of course a problem arises is you have a comment in the unwanted code.

Thus, it is better to do this

#ifdef OLD
     unwanted code
#endif

Note that if there is no #define OLD, the unwanted code is not passed to the compiler.