Logowww.asingh.netHomeNewsBioInterestsTechnicalSearchContact



 Archive :

  May 2006
  June 2006
  July 2006
  July 2007
  August 2007
  April 2008
  June 2008
  December 2008
  January 2009
  March 2009
  June 2009
  January 2010
 

C Macro For Printing Filename And Line Number

Most C compilers provide the macros __FILE__ and __LINE__ that can be used to print the source file name and line number respectively. These macros come in handy when printing debug information or when using printf() as a general purpose tool for debugging. Imagine a program that allocates memory via malloc(). As a good programming practice, before using the pointer returned by malloc() it has to be ensured that the allocation succeeded (i.e. the pointer returned by malloc() is not NULL). If the memory allocation fails, the program will probably have to give a message indicating this. Now, there can be several malloc() calls through out the program - which one failed? Well use of __FILE__ and __LINE__ macros make it easy to find.

Similarly, a statement like: "printf("%s:%d\n", __FILE__, __LINE__); " , if used appropriately can be used to follow the flow of a program. Ofcourse, this is a crude way of doing something that can be easily done through any standard debugger, without having to change code. Nonetheless, it can come in handy depending on the situation.

posted by Amandeep 5/15/2006 11:19:00 PM | PERMALINK

 

 

| Home | News | Bio | Interests | Technical | Search | Contact |

This page is powered

by Blogger.