This page has been designed specifically for the printed screen. It may look different than the page you were viewing on the web.
Please recycle it when you're done reading.

The URI for this page is { http://cc.byexamples.com }

print line help for debug Posted on January 18th

The simplest debug technique is to print out a line contains the required variables value. By printing out a line at a suspect bugs point, it helps for debug . With few magic variables, that ease us debug even more.

They are __FUNCTION__, __PRETTY_FUNCTION__, __LINE__, __FILE__ .

__FUNCTION__ will returns a string of the function name where the print line stay.

__PRETTY_FUNCTION__ will only recognized by gcc compiler, that returns function name with indication the function is from which class.

__FILE__ returns which source code filename.

__LINE__ returns the exact line of the print line.

How to use them? Easy, you just need a printf.

printf("[DEBUG] In %s(), triggers error ...\n",__FUNCTION__);

The same way to print the filename and the line like this:

printf("Errors appear after %s %s\n",__FILE__,__LINE__);

The newer compiler will also recognized __func__ which it is part of C99 standard, it works the same as __FUNCTION__.

Reference: http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html

Trackback URL

One Response to “print line help for debug” :

  1. This is extremely useful ;) Save a lot work to locate the buggy position.

    Commented vtian on January 21st, 2007.
Leave your own comments about this post: