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.
__FILE__
__func__
__LINE__
 
 
 
Example:
/tmp# cat x.c
main()
{
    printf("file='%s' \n function='%s' line='%d' \n",__FILE__,__func__,__LINE__);
}
/tmp# ./a.out
file='x.c'
 function='main' line='3'
/tmp#
 
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...