Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a C program

People also ask

What is debugging techniques in C?

Definition: The important technique to find and remove the number of errors or bugs or defects in a program is called Debugging. It is a multistep process in software development. It involves identifying the bug, finding the source of the bug and correcting the problem to make the program error-free.

Is debug hard C?

Compared to other programming languages, C can be a more difficult language to debug and figure out errors within code, no matter if it is logic or syntax-based.

What is GDB in C?

Gdb is a debugger for C (and C++). It allows you to do things like run the program up to a certain point then stop and print out the values of certain variables at that point, or step through the program one line at a time and print out the values of each variable after executing each line.


Compile your code with the -g flag, and then use the gdb debugger. Documentation for gdb is here, but in essence:

gcc -g -o prog myfile.c another.c

and then:

gdb prog

If you want a user-friendly GUI for gdb, take a look at DDD or Insight.


I guess that you are building from the command line.

You might want to consider an IDE (Integrated Development Environment), such as KDevelop or Eclipse, etc (hint - Eclipse ... ECLPISE ... E C L I PS E).

Use an IDE to edit your code, refactor your code, examine your code - class tree, click a variable, class or function to jump to declaration, etc, etc

And - of course - to debug:

  • run your code in the IDE
  • set breakpoints to stop at particular lines
  • or just step through, a line at a time
  • examine the call stack to see how you go there
  • examine the current values of variables, to understand your problem
  • change the values of those variables and run to see what happens
  • and more, more, more

p.s as wasatz mentioned- DDD is great - for visualizing the contents of arrays/matrices, and - imo - especially if you have linked lists


You can use gdb-based simple and useful GUI "Nemiver". It can debug your whole module comprising many source files.

enter image description here


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!