Debugging Using Preprocessor Directive in C
The C-compiler can detect only syntax errors whereas it cannot detect any run-time errors. So, this may produce an erroneous output if the run-time error persists.
To detect the run-time errors, the programmer needs to debug the program before execution. But this debugging process becomes a quite tedious task if the program is bigger containing many variables, functions, etc.
Hence, for bigger programs, the debugging can be done by inserting some print() statements in between the code that tells us about the intermediate results. Once done with the debugging, the printf() statements can be deleted.
But, after some time once again if it needs to debug the program, then it will be required to insert all those printf() statements again which will be again a tedious task. Hence to avoid this problem, preprocessor directives #define and #undef are used where the program can be debugged at any time as per our needs.
Using preprocessor directives, the debugging statements can be enabled or disabled as per our needs.
At this point, sum of a and b = 15 At this point, value of a = 6 value of b = 9
After the completion of the debugging process, we can remove the macro DEBUG by simply replacing the #define with #undef directive.
By such replacement, no debug statements will be compiled as all the #ifdef condition becomes false. In this way, the debugging process is done for the large programs using preprocessor directives.
The use of #ifdef and #endif for each debug statement seems to be lengthier. Hence to make the process more concise, we can use of another macro SHOW.
The macro SHOW is defined in such a way that when macro DEBUG is defined then this SHOW will be replaced by a printf() statement and if DEBUG is not defined then SHOW is not replaced by anything.
At this point, sum of a and b = 15 At this point, value of a = 6