Exposing program logic

  • Print statements inserted into the program code can print out the values of critically important variables

  • When a program runs, the listing of variable values can expose the logic of the program (and in doing so, may expose bugs in your code)

  • Once the print statements are inserted into the program code, it may be difficult to remove them...

  • My favorite coding aid is:

      • Use #ifdef clauses to enable/disable printf( ) statements

  • I often used different debug macro names to selectively activate/deactivate print statements that tracks different sets of variables

Example

  • Let's say you want to track:

      • the variable x which is important for situation A and
      • the variable y which is important for situation B

  • You would insert the following print statement at strategic locations (where the variables are used):

     #ifdef DEBUG_A
     printf("x = %d\n", x);
     #endif
     ..
     #ifdef DEBUG_B
     printf("y = %d\n", y);
     #endif
    

  • You can selectively activate/deactivate the printf( ) statement using the following compile options:

      gcc -g -o myProg -DDEBUG_A  -DDEBUG_B  myProg.c