![]() |
Chapter 7 | ![]() |
#include <stdio.h> int main(int argc,char *argv[]) { int a; int b=0; a= 3/b; return 1; }Create a new project in MSVC by going to File
Note that MSVC does not have an option for creating a 'C source file'. Instead, it only gives you an option to create a C++ source file. This isn't too incredibly problematic, just confusing - see, MSVC actually identifies files based on their extension and will use a different compiler for each file; a C++ compiler for .cpp files, and a standard C compiler for .c files. If you put a .c extension on your file, and then jump though a few hoops, it will recognize this .c file as a C source code file, and not a C++ one. If you write it outside of the IDE, and import it by using the Project a Add to Project a Files menu option, it will automatically select the C compiler instead of the C++ compiler. You'll notice though, that using the standard C compiler still often requires you to abide by the rules the C++ compiler has, such as use of keywords, though I'm told there are workarounds. It's annoying, but you're forced into the habit of making everything C++ correct, so get used to it. In the end, it's probably not a bad way to write code anyway - most C code should compile cleanly in a C++ compiler, just try not to mix the two types - though MSVC seems to do some automatic 'fixing' so groups of files created at the same time always use the same compiler unless you explicitly tell it otherwise.
In short, it's another case where it's easier to bend to the quirks of MSVC than to take the time and energy to 'fix' it.
Okay, back to work. You've got your code, it's saved in a file, now you're ready to debug it. First, compile it, using the build window we showed earlier (tip; build hotkey is F7). If you attempt to debug it without compiling it first, you will be prompted to build the program if it either doesn't exist, or is out of date. In either case, start the debugger (F5). You should get a screen like the following:
You may need to expand that picture up a bit, as it contains quite a bit of information, but you can also use the tool-tip popup help to identify the button/window I'm talking about. Tool-tip popup help is activated when you leave your mouse over one of the buttons for a short time, and in some cases, there will be a status message in the bottom bar.
Now, here's what these controls do:
Active Window List:
You can get this by right clicking in any blank area in your menu bar space (Note that you can move the menus
around, and even tear them off if you're so inclined). What you see selected on the list currently is a standard
debugging window view. If you ever 'lose' a menu or window, check here first.
Build Minibar:
You already learned all about this in a previous section.
Call Stack Display:
This small window displays the call stack for the current program. Because of the way MSVC handles things,
you're always going to be starting at stack level 3, which you can see as the topmost line with a yellow arrow
pointing to it (calling 'main()'). Clicking on a given line switches the current context to that stack (more on this later).
Variable Window Display:
This window will display all variables which are defined within the function we're currently stopped on.
Breakpoint/Execution bar:
This bar is incredibly helpful, as it contains a bright yellow arrow pointing you to the line at which
the most recent error occurred. If you set breakpoints (covered later) these too will show up here.
Watch Window Display:
With the Watch Window, you can also type in variable names which will be resolved if they're valid - for example,
global variables, or function pointers.
Output Window Display:
Your new best friend. Anything that happens with the compiler and IDE (compiling, linking, using 'find', errors, etc) happens in these
windows. Also, using the TRACE macro inside your code will display messages in the debug window (only if you are running the
software in debug mode).
Debug Window:
As you can see, the Debug window has alot of buttons in them.. The top row have to do with running the debugger, the bottom
have to do with displaying the different windows - such as the watch window, and quickwatch window (which are prety much the same
thing).
Running the debugger
![]() |
Index | ![]() |
7.3 Stopping the MSVC Debugger | 7.4.2 Examining Variables |