Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to debug a c or c++ program?

  1. Apr 15, 2006 #1
    I learn to code in the c language. i am learning the workings of C++, and Java.

    I already have a c/c++ compiler in my computer. My problem is:
    How do i "check for errors"/debug a c or java program? Are there any good tutorial online?
  2. jcsd
  3. Apr 15, 2006 #2
    what compiler are you using? if they're small codes "printf"(for c) is your friend
  4. Apr 16, 2006 #3
    If the code is small, use printf as he said, but follow these printf calls with the line:


    Otherwise if your program crashes in runtime it might not print everything that it is supposed to (output is buffered, and fflush clears out the buffer). You have no idea how confusing it can be to think a program is failing at point A when it is actually failing a little later at point B.
  5. Apr 16, 2006 #4


    User Avatar
    Science Advisor

    Java programs are easier to debug than C programs. You can use System.out.println to print debugging messages. If your program is crashing then you can follow the stack trace to see where the exception originated. You can also use try{}catch(Exception e){...} throughout your code. One reason why i love Java.
  6. Apr 16, 2006 #5
    youar.Master: what does the fflush(stdout) display? i've never seen that before(i'm a novice or inter. programmer).
  7. Apr 21, 2006 #6
    fflush(stdout) doesn't display anything as Your.Master said, it locks the program and forces data sitting in the stdout buffer to be written which ensures that you will see output from the printf. For example if you have a printf line followed immediately by a line that causes a segmentation violation, the program may receive a signal to kill it before the previous lines printf has been flushed, this may lead you to think the program is crashing somewhere before that printf. For more information 'man fflush'.
  8. Apr 22, 2006 #7


    User Avatar
    Staff Emeritus

    This is specific to unix/linux, but when I debug code I use a combination of printf statements as well as compiling the program with a -g option in gcc and then using DDD to trace variables and such.


    ddd is really nice for tracking linked lists because it will actually show you graphically how the nodes are linked and what information they contain.

  9. Apr 22, 2006 #8


    User Avatar
    Science Advisor

    That's very nice, does it do other data structures, like trees?
  10. Apr 22, 2006 #9


    User Avatar
    Staff Emeritus

    Yeah, it can handle pretty much any data structure you feed it.

    Here is an example of trees (I've had to scale it down a lot because of my 1600x1200 resolution:

    Attached Files:

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: How to debug a c or c++ program?
  1. C Program (Replies: 3)

  2. C program (Replies: 0)

  3. C programming (Replies: 9)