Thread: I need a function that can debug

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    1

    I need a function that can debug

    Hi,

    I am new to this message board and I am a newby and I know I will be needing some help. I need a function that can debug the program from within the code to let me know where the break or error happen. Is there something like that you can call on to be put in a program.

    Thanks

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    It's called printf(). You shove them all over your program.

    Code:
    printf("The program made it to this point.");
    Otherwise you can just use a debugger. You might have gotten it with your compiler.
    Sent from my iPadŽ

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Some useful defines for debugging are:
    Code:
    __LINE__  /* current line of the source file (<underscore><underscore>LINE<underscore><underscore>) */
    __FILE__  /* current source file name */
    __TIME__  /* the time this executable was compiled (string) */
    __DATE__ /* the date it was compiled (string) */
    __func__  /* (C99) name of the current function */
    So you could have a debugging macro like this:
    Code:
    #include <stdio.h>
    
    #define debug_str(s) \
        printf(__FILE__ ":" __func__ "():%i: %s\n", __LINE__, s)
    
    int main(void) {
        debug_str("Before printf()");
        printf("Hello, World!\n");
        debug_str("After printf()");
    
        return 0;
    }
    Output:
    Code:
    debug_str.c:main():7: Before printf()
    Hello, World!
    debug_str.c:main():9: After printf()
    The best debugging tool, however, is a debugger. If you have Dev-C++ or GCC, then you can compile your program with -g and use GDB to debug it:
    Code:
    C>gcc -g -o hello.exe hello.c
    
    C>gdb hello.exe
    This is GDB version x.xx ....
    (gdb) run
    Hello, World!
    
    The program exited normally.
    (gdb) quit
    C>
    This site has a tutorial on using GDB.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM