0
What is WATCH in C++?
I have seen a lot of WATCH(someVariable) within a C++ code. Does anyone have an idea about what it is?
4 Answers
+ 2
for using watch first you have to get into debugging moe.For that begin by using the āStep intoā command.
In Visual Studio 2005 Express, go to the debug menu and choose āStep Intoā, or press F11.
If you are using a different IDE, find the āStep Intoā command in the menus and select it.
When you do this, two things should happen. First, because our application is a console program, a console output window should open. It will be empty because we havenāt output anything yet. Second, you should see some kind of marker appear to the left of the opening brace of main. In Visual Studio 2005 Express, this marker is a yellow arrow. If you are using a different IDE, you should see something that serves the same purpose.
+ 2
This arrow marker indicates that the line being pointed to will be executed next.
+ 2
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "stdafx.h"
#include <iostream>
int main()
{
Ā Ā Ā Ā int x =1;
Ā Ā Ā Ā std::cout << x << " ";
Ā
Ā Ā Ā Ā x = x + 1;
Ā Ā Ā Ā std::cout << x << " ";
Ā
Ā Ā Ā Ā x = x + 2;
Ā Ā Ā Ā std::cout << x << " ";
Ā
Ā Ā Ā Ā x = x + 4;
Ā Ā Ā Ā std::cout << x << " ";
Ā
Ā Ā Ā Ā return 0;
}
This is a pretty straightforward sample program -- it prints the numbers 1, 2, 4, and 8.
First, use the āRun to cursorā command to debug to the firstĀ std::cout << x << " ";Ā line:
ļæ¼
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. Most modern debuggers support this method of inspecting simple variables, and it is the most straightforward way to do so.
ļæ¼
Note that you can hover over any variable x, not just the one on the current line:
ļæ¼
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. First, choose āStep overā twice, so the next line to be executed is the secondĀ std::cout << x << " ";Ā line:
ļæ¼
The variable x should now have value 2. Inspect it and make sure that it does!
+ 1
it was so big explanation so copied last part from google.hope you understood it.