From the course: C: Data Structures, Pointers, and File Systems

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Debugging your code

Debugging your code - C Tutorial

From the course: C: Data Structures, Pointers, and File Systems

Start my 1-month free trial

Debugging your code

- [Instructor] When I'm at my wits' end and I just can't figure out what's wrong with my code, I use a few simple tricks to examine the running code and fix the bugs. These including outputting values as the program runs, adding text flags at various points in the code, writing myself notes in the comments, and reading the code aloud. First, you can track values throughout your code by using temporary printf statements. Output the value of a variable, for example, before and after a function call to ensure that the result is what you believe. For pointers, remember that you can use the %p placeholder to output an address. Check that pointer's value to ensure that it's being adjusted or saved in the way you want. Use the __LINE__ preprocessor directive to output specific line numbers in the source code. This directive uses two underscores on either side of the word LINE, all caps. For example, this printf statement would tell me where a problem may be occurring. Another message you can…

Contents