3.8 — Using an integrated debugger: Watching variables

In the previous lessons (3.6 -- Using an integrated debugger: Stepping and 3.7 -- Using an integrated debugger: Running and breakpoints), you learned how to use the debugger to watch the path of execution through your program. However, stepping through a program is only half of what makes the debugger useful. The debugger also lets you examine the value of variables as you step through your code, all without having to modify your code.

As per previous lessons, our examples here will use Visual Studio -- if you are using a different IDE/debugger, the commands may have slightly different names or be located in different locations.


In case you are returning, make sure your project is compiled using a debug build configuration (see 0.9 -- Configuring your compiler: Build configurations for more information). If you’re compiling your project using a release configuration instead, the functionality of the debugger may not work correctly.

Watching variables

Watching a variable is the process of inspecting the value of a variable while the program is executing in debug mode. Most debuggers provide several ways to do this.

Let’s take a look at a sample program:

This is a pretty straightforward sample program -- it prints the numbers 1, 3, and 6.

First, run to cursor to line 6.

At this point, the variable x has already been created and initialized with the value 1, so when we examine the value of x, we should expect to see the value 1.

The easiest way to examine the value of a simple variable like x is to hover your mouse over the variable x. Some modern debuggers support this method of inspecting simple variables, and it is the most straightforward way to do so.

For Code::Blocks users

If you’re using Code::Blocks, this option is (inexplicably) off by default. Let’s turn it on. First, go to Settings menu > Debugger…. Then under the GDB/CDB debugger node, select the Default profile. Finally, check the box labeled Evaluate expression under cursor.

Hover your mouse cursor over variable x on line 6, and you should see something like this:

Note that you can hover over any variable x, not just the one on the current line. For example, if we hover over the x on line 12, we’ll see the same value:

If you’re using Visual Studio, you can also use QuickWatch. Highlight the variable name x with your mouse, and then choose “QuickWatch” from the right-click menu.

This will pull up a subwindow containing the current value of the variable:

Go ahead and close QuickWatch if you opened it.

Now let’s watch this variable change as we step through the program. Either choose step over twice, or run to cursor to line 9. The variable x should now have value 3. Inspect it and make sure that it does!

The watch window

Using the mouse hover or QuickWatch methods to inspect variables is fine if you want to know the value of a variable at a particular point in time, but it’s not particularly well suited to watching the value of a variable change as you run the code because you continually have to rehover/reselect the variable.

In order to address this issue, all modern integrated debuggers provide another feature, called a watch window. The watch window is a window where you can add variables you would like to continually inspect, and these variables will be updated as you step through your program. The watch window may already be on your screen when you enter debug mode, but if it is not, you can bring it up through your IDE’s window commands (these are typically found in a View or Debug menu).

For Visual Studio users

In Visual Studio, the watch menu can be found at Debug menu > Windows > Watch > Watch 1. Do note that you have to be in debug mode for this option to be enabled, so step into your program first.

Where this window appears (docked left, right, or bottom) may vary. You can change where it is docked by dragging the Watch 1 tab to a different side of the application window.

For Code::Blocks users

In Code::Blocks, the watch menu can be found at Debug menu > Debugging windows > Watches. This window will likely appear as a separate window. You can dock it into your main window by dragging it over.

You should now see something like this:

The watches window may or may not contain anything in it already.

There are typically two different ways to add variables to the watch window:

  1. Pull up the watch window, and type in the name of the variable you would like to watch in the leftmost column of the watch window.
  2. In the code window, right click on the variable you’d like to watch, and choose Add Watch (Visual Studio) or Watch x (replace x with the variable’s name) (Code::Blocks).

If you’re not already in a debugging session with the execution marker on line 9 of your program, start a new debugging session and run to cursor to line 9.

Now, go ahead and add the variable “x” to your watch list. You should now see this:

Now step over twice, or run to cursor to line 12, and you should see the value of x change from 3 to 6.

Variables that go out of scope (e.g. a local variable inside a function that has already returned to the caller) will stay in your watch window, but will generally either be marked as “not available”, or may show the last known value but grayed out. If the variable returns to scope (e.g. the function is called again), its value will begin showing again. Therefore, it’s fine to leave variables in the watch window, even if they’re out of scope.

Using watches is the best way to watch the value of a variable change over time as you step through your program.

The watch window can evaluate expressions too

The watch window will also allow you to evaluate simple expressions. If you haven’t already, run to cursor to line 12. Then try entering x + 2 into the watch window and see what happens (it should evaluate to 8).

You can also highlight an expression in your code and then inspect the value of that expression via hover or by adding it to the watch window via the right-click context menu.


Identifiers in watched expressions will evaluate to their current values. If you want to know what value an expression in your code is actually evaluating to, run to cursor to it first, so that all identifiers have the correct values.

Local watches

Because inspecting the value of local variables inside a function is common while debugging, many debuggers will offer some way to quickly watch the value of all local variables in scope.

For Visual Studio users

In Visual Studio, you can see the value of all local variables in the Locals window, which can be found at Debug menu > Windows > Locals. Note that you have to be in a debug session to activate this window.

For Code::Blocks users

In Code::Blocks, this is integrated into the Watch window, under the Locals node. If you don’t see any, there either aren’t any, or you need to uncollapse the node.

If you’re just looking to watch the value of a local variable, check the locals window first. It should already be there.

3.9 -- Using an integrated debugger: The call stack
3.7 -- Using an integrated debugger: Running and breakpoints

112 comments to 3.8 — Using an integrated debugger: Watching variables

  • i ran first example using Code Blocks, watching variable and got the following error message:

    ||=== Build: Debug in watching)a_variable (compiler: GNU GCC Compiler) ===|
    C:\CPP_Program_froadd_filesm_B00k\watching)a_variable\main.cpp|2|fatal error: stdafx.h: No such file or directory|
    ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

    Why is stdafx not a file?

  • ukijo

    when i try to debug the porgram the AV consider it as a virus ?? help pls

    • Alex

      Some Antivirus programs don't like it when programs create other executable programs, because it's a potential security issue. Turn off your AV while you're compiling.

  • AMG

    "In order to address this issue, all modern compilers provide another feature, called a watch window." I thought IDE does it. Could you please explain a little bit more? Thanks.

  • Dino

    Hey Alex, I am using Bloodshed DevC++, but I can't seem to get it to display the call stack.Maybe I can't find it.
    I see people trashing Bloodshed online, but I'm kind used to it now since I've been using it on my university about 7 years ago so I'm comfortable with it. However, I can change to a different IDE if you think some other one might be better for this course. I've read that Blood was kinda outdated.

    • Alex

      I wouldn't recommend Bloodshed to any new users, but if you're already familiar with it, it looks like the latest version supports C++11 ( I can't speak to the call stack issue, as I've never used Bloodshed and am not familiar with it. Are you using the latest version? If not, you should at least upgrade.

  • Cloud

    Hey, Alex. I used the same code as above but when I examined the variable "x" in debug mode, it showed a much bigger number instead of its actual value

  • Hi Alex,
    In the project i have 2 files with different function name but with same variable, when i add it to debug"add watch" it allow to add only one of 2. How can i show to debug, that this variable are with the same name but from different functions ?

    • Alex

      You don't need to. Let's say you have two functions, A and B, both of which contain a variable named X. You add X to the watch list. From that point forward, X will refer to whichever X is in scope at the moment.

      That means when you're inside a function call to A, the X on the watch list will refer to A's X. When you're inside a function call to B, the X on the watch list will refer to B's X. When you're outside A or B, X won't refer to anything, since no variable X is in scope.

  • Alex

    Thanks Alex for these greate tutorials..
    Actually i am trying to make plugins for maya and i have set alll the ideas and details and it needs to be made in c++ .. so your tutorials come in handy.. thank u

  • Strong

    hi alex,

    how can i watch my 2d array in gdb terminal ?
    i can watch my 1d array with  : display *[email protected] . but IDK HOW to set the column and row to display in 2d array

  • Kiran C K

    Hey Alex,
    This is my second round of going through this entire tutorial. I am trying to grasp what I have missed / forget / to become more convenient with the language. I find debuggers to be very useful with procedural programming. But in OOPS, it seems to be not of much use. Can you comment on this? I am asking this question way ahead of OOPS lesson because debuggers are not discussed after this lesson.

    • Alex

      I'm curious as to why you think debuggers are less useful in OOP. I find them to be just as useful (maybe more so in some cases, since it's not always obvious what function is being called when you have virtual functions).

      • Kiran C K

        I am adding one of the example programs that you have added in one of the future lessons.

        When I used the C++11 debugger using codeblocks, all that the debugger points to is main and when I click on 'Step into' it points to the child class and that is it. I had just 2 debugging steps I had for the entire program. But in procedural programming, it points to each instruction

        • Alex

          I think you may be doing something incorrectly. When I debug this program and press step-into (f11) in Visual Studio 2015, it stops at the following lines:
          * { (in main)
          * Derived cDerived
          * Base &rBase = cDerived
          * std::cout << "rBase is a " << rBase.GetName() << std::endl; * Derived::GetName() * The template for operator<< (at this point, I step out) * The template for operator<< (at this point, I step out) * return 0; * } (main)

  • Paul

    Hey Alex, I want to say thank you for the phenomenally excellent tutorial on how to learn C++, I've tried multiple other tutorials for various different languages, including C, Python, C#, BASIC, Java and probably others I've forgotten, and none of them have been as easy to follow as this.

    I'm a fast learner, I pride myself on that, and I can learn from reading, hands on practice, and being shown how to do something, but no other learning resource for programming has helped me grasp the concepts as well as yours has.

    I was considering giving up trying to learn programming at all until I came across this website, and being explained concepts thoroughly before being asked to use them has boosted my confidence so much, and I've learned more in the last 4 days than I have in the last year since starting to learn programming.

    I have been able to implement an if else function in a practice calculator designed to catch a user trying to divide by zero, and I have been toying with trying to catch an exception with a user entering the wrong option when choosing the operand, I have implemented it, but it doesn't catch the error until after trying to perform the calculation instead of when the user enters the option for the desired operand, and it doesn't ask the user to try again, it's included below in the else if statement.

    The divide by zero if else statement isn't perfect, but it does return an error message to the user, and returns -1 when the error is caught, my attempt is below if anyone is interested.

    I'm sure many smarter people than me will point out how to improve this, and honestly I welcome it, but for something that I've done by myself, I'm very proud of it and the rest of the code for the calculator, before reading your tutorials doing that trying to implement that simple bit of code would have frustrated me and left me wanting to quit, so the fact I can even do something this simple after less than a week of learning with your tutorial is a source of pride for me.

    In fact I just realised that I can improve the above code by using || instead of using multiple && for the else if statement.

    I'm even more proud of myself for that.

    my very best regards, Paul

    • Alex

      I'm glad you're finding the tutorials helpful.

      One quick note:

      evaluates as:

      Which isn't what you want. You probably meant:

      For readers who don't know what operator || and && are, we cover them in chapter 3. :)

      • Paul

        Yeah, you're right, I'll note that for future use, but that else if statement was something I sort of figured out myself and edited it in just before I hit post.

        I already had some prior knowledge of || and && from other programming tutorials before I posted that, but as you can see, I'm still not quite there yet, so thanks for pointing that out, it might also help others who're reading these lessons, and help them to avoid a similar, albeit minor, mistake.

        In any case though, I am thoroughly loving this series, I will admit that I was starting to struggle with the stuff in chapter 4, so I decided to return back to chapter 1 and go over it again and reinforce the concepts covered until I get back to where I was.

        I really need to learn to slow down instead of going ahead when something clicks.

        I was just wondering though, wouldn't you be able to remove some of the redundancy from that else if statement by typing

        instead of typing "operand !=" multiple times?

        I have a feeling that it would have a very different meaning to the compiler than what your version does, but I wanted to be sure from someone more knowledgeable than me. :D

        Thanks for the reply! :)

        • Alex

          evaluates as:

          which evaluates to:

          Probably not what you had in mind.

  • sdmv

    I have a doubt
    When i set "run to cursor" to the beginning of the x variable assignment instead of the first cout statement the watch shows the value of x as -858993460.
    Why does this happen?

    • Alex

      858993460 is 0xCCCCCCCC in hex. When you run a program in debug mode, C++ initializes stack memory to this value. Since your variable is declared on the stack (we'll explain what this means in the future), it gets the value 0xCCCCCCCC by default. In release mode, this doesn't happen.

  • raj

    hi there,
    thank you for your tutorials. while i was learning this debugging chapter i followed your instructions but the thing is when i follow your instructions exactly i am not able to understand this topic(may be because i am usng the latest ver of codeblocks 16.01 ) and the explanation u have given is mainly for VS. can u please also add the explanation to CB also along with some images.

  • Derek

    My Call Stack window has this listed:
       Calling.exe!CallC() Line 8
       Calling.exe!CallB() Line 16
       Calling.exe!CallA() Line 21
       Calling.exe!main() Line 28
       [External Code]
       [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]

    Double-clicking on each of those lines in the Call Stack window essentially takes me to the line it said it would (e.g. double-clicking Calling.exe!CallC() Line 8  takes me to Line 8 of the code). I'm confused, however, on why those lines were chosen. Line 8 is the opening brace for function CallC(), Line 16 is the closing brace for function CallB(), and Line 21 is the call to CallC() in function CallA(). To me, I don't see any similarity or pattern between those lines, so I'm curious as to why Call Stack would take me those certain parts of the code.

  • Nyap

    I keep getting "cannot open file: fileops.c" from codeblocks when I try doing anything with the debugger ;(
    can't find anything online about it either ;(

    • Alex

      Unfortunately, this is beyond my knowledge. :(

    • Nyap

      if anybody else happens to have the same issue, I've found a solution (sort-of)
      put a breakpoint on the first line of all the cpp files and it sort of works but might behave weird every now and then
      I wouldn't class it as "a good debugging solution" though, but it will be fine for small programs

  • Brian

    One final note regarding your example with respect to Call Stacks. (Having written instructions with diagrams for others to follow as a System's analyst, I know fixing it may not be worth the effort.) The code that you give for your example Test2 and the line numbers shown by the example image of the Call Stack window do not match. Doing exactly as you had written, I found that the first break-point added within the CallC function pauses the code is at line 6, and not line 9 as depicted by your example Call Stack Window image. (Picky I know, but I strive to keep all confusing aspects when demonstrating code to others to a minimum, thus reducing the number of trivial questions.)

    • Alex

      Thanks, I added a note that your line numbers may differ slightly, to let users know that this is not important to the example. I appreciate the feedback.

  • Brian

    Alex, just wanted to say first off, great tutorials. After my 10 and 12 year old children finish learning how to type properly, I am putting them on to your web site to learn about programming. With that in mind, I'd like to make a simple suggestion regarding your instructions. Specifically with regards to the following line.

    'Now let’s watch this variable change as we step through the program. First, choose “Step over” twice, so the next line to be executed is the second std::cout << x << " "; line:'

    You might want to change it to:

    'Now let’s watch this variable change as we step through the program. First, close the Quick Watch window and choose “Step over” twice, so the next line to be executed is the second std::cout << x << " "; line:'

    My only reason is, I had thought the Quick Watch window could remain open and I could proceed by switching focus to the code and stepping through the program, but quickly realized I couldn't. I'm looking forward to putting my kids unto your web site and I'd hate for them to get frustrated when reading and following your instructions using the IDE. (I'm new to IDEs as well.) I will be following your tutorial to the bitter end and appreciate all your hard work. Keep up the great work. (You should write a tutorial on how to write well written and superbly organized tutorials. :))

  • AlexM

    Hi Alex, and first of all thanks for this site!

    Posible I`m wrong but I have to ask:  this code  I write  below gives me some error ...

    ..cpp(13): error C2374: 'x': redefinition; multiple initialization.
    and so on in all the lines the x apears.
    Is this a problem for debuging?

    Thanks again!

    • Alex

      Yes, it's a problem for debugging. Compiling your program is a prerequisite for debugging it. Consequently, you'll need to fix up your program first.

  • Sergio Ramos

    Ok, i missed something... what is this?

    int x(1);

    Function declaration passing an argument without any defanition? Does that just return 1 since there is no body to it, yet it is passing 1 into nothing? And what section was this convention introduced to declare "int x = 1;"?

    • Alex

      My bad. I moved this tutorial from later in the series, when I'd already introduced that concept. It's the equivalent of int x = 1.

      I've updated the example.

      I discuss this form of initialization at the beginning of chapter 2 (you're so close!).

  • I use Xcode for programming in C++ and what's kinda strange is that you cannot enter debug mode directly for command line projects, almost all menu items below Debug stay disabled (you cannot click "step into" like in Visual Studio to start debugging, cause it's disabled while you are not in debug mode yet). So what I do every single time I want to debug a Command Line project, is entering the debug mode by using breakpoints. So by adding a breakpoint at some row (e.g. before "int main()", where the project starts), I can enter the debug mode from there by starting the project.

    So after starting the project, it stops at the breakpoint and I can use all debug tools from there:
    - the watch window
    - the steps (step in, over, out)
    - the call stack window
    - etc....

    Hopefully this helps anybody who's using Xcode, who finds out that all debug menu items are disabled.
    Or if somebody has a better idea to start debug mode, let me know.

  • huehuHUEHUE

    Behaviour in codeblocks is weird, the call stack gives a weird name to one of the functions "#1 0x40143d    _fu1___ZSt4cout() (C:\CBProjects\HelloWorld\main.cpp:10)"

    I was expecting it to say CallB() there.

    • Alex

      When the compiler compiles functions, it mangles the names of the functions (so it can do function overloading, a topic we'll talk about later). Code::blocks may be showing you the mangled names.

  • Bogche

    Thanks Alex you are one of the "GREATH TEACHERS" of the world.

  • Zak

    I never knew programming will be so easy until I came accros website .... Thanks a millions Alex

  • 321123

    After first breakpoint when you want to make you program keep running until a breakpoint is hit again.
    Don't use F11, you will step into the Ostream's code.
    Just hit F5(Continue) and the program will run until it hit breakpoint again.

    Maybe the F5(continue) hotkey should be mentioned in this or the previous part of the guide when "Continue" is mentioned.
    It's easy to confuse continue with F11(step in) that we used earlier.

  • Aiden

    Oops, now I see my problem with the adding, but I still wonder why does it say it stopped working?

    • Alex

      I think it’s crashing because you’ve declared your variables as parameters to main, which is confusing the compiler. Main should not have any parameters, and the variables should be declared locally.

      e.g. instead of:

      Do this:

  • Aiden

    Hey Alex, The question I have isn't related to this lesson, but i just thought of posting it. Anyway, i wrote this code

    #include "stdafx.h"
    #include <iostream>

    int add(int n, int b);

    int main(int x, int y, int n, int b)
        using namespace std;

        cout << "Enter a number: ";

            cin >> x;
            cout << "Enter a number : ";
            cin >> y;
            cout << "Enter another: ";
                cin >> n;
                cout << "One more: ";
                    cin >> b;
                cout << x << " + " << y << " + " << b << " + " << n << " = " << x + y + b + n << endl;

        return 0;

    int add(int n, int b)

        using namespace std;

        cin >> n;
        cout << "Enter another: ";
        cin >> b;
        cout << "One more: ";

        cout << n << " + " << b << " = " << endl;

        return 0;

    and when I run it, and I type in all  the number for it to add, it gets mixed up. Say i put in 5, 4, 4, 3 It would add 5, 4, 3, 4 which is fine, but i was just wondering what i was doing wrong? and after i put in every number, it says the program has stopped working, But when i click close program, it goes right back and says press any key to continue like normal.

  • Aroma

    Alex ,is a great job you are doing here!, Thanks so much for this wonderful tutorials, am really enjoying programming by your courtesy. Thanks a million!

  • Alpan

    I never saw an expression like "int x(1);"
    Is that correct syntax-wise?

    • Alex

      Yes, it's correct.

      This does an implicit initialization of variable x. Initializing variables this way in C++ is more commonly seen with classes than fundamental types like integers, but there are good reasons to use it for the fundamental types too.

      • Chris H

        Is this considered as forward declaring variables? I remember reading this in a previous section.

        • Alex

          No. The statement defines (instantiates) a variable and initializes it with a value.

          The following two statements are nearly identical for fundamental types:

  • Mustho

    If one is not interested in debugging, he may not start programming. Debugging is what programming is all about!

  • Simon

    Somehow when i'm debugging using the Step Into method (F11 in Visual Studio 2013) I sometimes I get a (seemingly) random long block of code until I click back on my cpp file. Any suggestions?

  • Tang

    Alex, thank you very much. Your lessons are very interesting and very useful for me.

Leave a Comment

Put all code inside code tags: [code]your code here[/code]