Skip to content
Related Articles

Related Articles

Debugging Using Preprocessor Directive in C

View Discussion
Improve Article
Save Article
  • Last Updated : 02 Aug, 2022
View Discussion
Improve Article
Save Article

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.

Example:

C




// C program to demonstrate the 
// debugging process using
// preprocessor directive '#define'.
#include <stdio.h>
#define DEBUG
  
int main()
{
    int a = 5, b = 10, sum = 0;
    sum = a + b;
  
#ifdef DEBUG
    printf("At this point,\nsum of a and b = %d \n", sum);
  
#endif
    a++;
    b--;
  
#ifdef DEBUG
    printf("\nAt this point,\nvalue of a = %d\n", a);
    printf("value of b = %d", b);
#endif
}

Output

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. 

Example:

C




// C program to  demonstrating the debugging process using
// preprocessor directive '#define' and macro 'SHOW'.
  
#include <stdio.h>
#define DEBUG
  
#ifdef DEBUG
#define SHOW printf
#else
#define SHOW // macros
#endif
  
int main()
{
    int a = 5, b = 10, sum = 0;
  
    sum = a + b;
  
    SHOW("At this point,\nsum of a and b = %d \n", sum);
  
    a++;
    b--;
  
    SHOW("\nAt this point,\nvalue of a = %d\n", a);
    ("value of b = %d", b);
}

Output

At this point,
sum of a and b = 15 

At this point,
value of a = 6

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!