While debugging C program, we often add printf's to print some values or just a debug print. It would be additional advantage if you can print line number and function name when the program is too big to debug. If you are dealing with multiple files, its good idea to print the file name when you use printf isn't ?
In C, there are few preprocessor macros which allow you to print line numbers, function names and files names.
__LINE__ : Prints line number
__FUNCTION__: Prints function number
__FILE__ : Prints file number
These are predefined macros and part of the C/C++ standard. During preprocessing, they are replaced respectively by a constant string holding an integer representing the current line number and by the current file name.
#include <stdio.h>
int main()
{
printf("\n This program prints line number %d\n",__LINE__);
printf("\n This program prints function name %s() \n",__FUNCTION__);
printf("\n This program prints file name %s\n",__FILE__);
return 0;
}
Output of this program:
Others preprocessor variables :
mrtechpathi@mrtechpathi:~/Study/C/K_and_R$ ./a.out
This program prints line number 5
This program prints function name main()
This program prints file name print_line_function_file_name.c
Others preprocessor variables :
__func__ : function name (this is part of C99, not all C++ compilers support it)
__DATE__ : a string of form "Mmm dd yyyy"
__TIME__ : a string of form "hh:mm:ss"
No comments:
Post a Comment