Friday, March 18, 2011

C Developer tip - Printing source info in C Programme

Lets start soemthing intresting which is commonly useful for C developers.
if you want to print filename, function name and line number  in a c programme, you can make use of following macros.
/tmp# cat x.c
    printf("file='%s' \n function='%s' line='%d' \n",__FILE__,__func__,__LINE__);
/tmp# ./a.out
 function='main' line='3'
This is very useful while programme is in panic and  you want to exit gracefully.
For example: malloc not succeeded.
Pin It
Related Posts Plugin for WordPress, Blogger...