How to Examine Variables with Code::Block Debugger for C Programming - dummies

How to Examine Variables with Code::Block Debugger for C Programming

By Dan Gookin

Sometimes, the problem with your C programming instructions can’t be determined by merely watching program flow. Nope, you have to get down and dirty in memory and look at a variable’s value while the code runs. The Code::Block’s debugger allows you to watch any variable in a program, showing you that variable’s contents as the program runs.


#include <stdio.h>
#include <stdlib.h>
int main()
  int x;
  int *px;
  return 0;

Exercise 1: Create a new Code::Blocks project with debugging active. Copy the source code above into the main.c file. Build and run. See whether it works.

It doesn’t, or it shouldn’t, unless you mistyped something. Time to debug! Follow these steps:

  1. Click the mouse to place it at the start of Line 6, where the integer variable x is declared.

  2. Click the Run to Cursor button on the Debugging toolbar.

  3. Click the Debugging Windows button.

  4. Choose the Watches command.

    The Watches window appears.


  5. Click the mouse in the first blue box in the Watches window.

  6. Type x for variable x and press the Enter key.

  7. Type px on the next line to examine the memory location stored in the px pointer. Press the Enter key.

  8. Type *px on the third line to examine the contents of the memory location stored by pointer px. Press the Enter key.

    You may see values appear for those variables, although the values are uninitialized. For example, on the screen, variable x holds the value 56, which is whatever random garbage appears in memory before the variable is assigned a value.

    Until a variable is initialized, its contents are junk.

  9. Click the Next Line button on the Debugging toolbar until the cursor is on Line 10, the start of the for loop.

    As the value of pointer px is assigned to the memory location of variable x, pay heed to the Watches window. Instantly, you see a memory address appear by variable px, and you see the *px variable set equal to whatever value is assigned to variable x. The pointer has been initialized!

  10. Click the Next Line button again.

    As the for loop starts, it initializes the value of variable x. You see that value change in the Watches window, along with the value of *px. The value of px (the address of variable x) doesn’t change.

  11. Continue to step through the code to watch the values change.

  12. Click the Stop button when you’re done.

Examining variables in memory is yet another good way to see what’s going on with your code. If the variables aren’t popping the way they should, you need to check the statements manipulating those variables.

Also, by examining pointers in memory, you can get a better idea of how pointers work.