Debugging using C macros __FILE__ and __func__ and __LINE__

FOllowing program shows a sample example of how we can use C macros __FILE__ , __func__ and __LINE__ to print the debug information which will show us, where is execution reached to, if any error it can print the line number of error and if in case of multi file programs like library, it can even show which file has some issues or we can also use it for verbose message during the development.

#include <stdio.h>
#define DEBUG_THIS printf("debug statement, in \n\tfile : %s, \n\tfunction: %s, \n\tLine : %d \n", __FILE__, __func__, __LINE__);
void debug_function (void) { printf("debug statement, in \n\tfile : %s, \n\tfunction: %s, \n\tLine : %d \n", __FILE__, __func__, __LINE__);
}
int main (int argc, char **argv) { printf("Main function, calling debug\n"); debug_function(); printf("back to Main\n"); DEBUG_THIS; return 0;
}

In above program, we have written a function which when called prints the debug information or you can also use a macro DEBUG_THIS whenever you want to print the messages.

Leave a Reply

Your email address will not be published. Required fields are marked *