Important C and C Header Files

Chapter 3 - Writing, Compiling, and Debugging Simple Programs

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Debugging Programs
If your program contains syntax errors, executing a Build or Rebuild All command automatically opens the compiler output message window, as seen at the bottom of the screen in Figure 3-8.
Figure 3-8: The Visual C++ compiler’s window for viewing a program’s syntax errors
Each message begins with the source file’s name, which for this example is C:\Program Files\DevStudio\MyProjects\Error\ERROR.C. This filename is important, because the typical Windows application contains many source files.
  Note You will probably have your monitor’s screen resolution set high enough to actually view the entire error or warning messages described in the following text.
Immediately to the right of the source file’s name is the line number, in parentheses, in which the warning or error was detected. In our example, the first error message was generated on line eight (8). To the right of the line number is a colon, followed immediately by the word “error” or the word “warning,” which is then followed by the associated error number.
Programs can run with warning messages, but not with error messages. The last piece of information found on each message line is a brief description of the detected syntax error.
Differences Between Warning and Error Messages
Warning messages might flag the use of a standard C/C++ automatic rule. For example, an automatic rule might be invoked when having a float value automatically truncated when assigning it to an integer variable. This does not mean that the code was written incorrectly, only that the statement is using some sort of behind-the-scenes feature of C/C++.
For example, all of the functions prototyped in MATH.H have formal arguments of the type double and return the type double. If your program passes to one of these functions an argument of the type float, the compiler will generate a warning. This warning will inform you that a conversion is taking place from the type float to the type double as the argument is pushed onto the call stack.
You can remove many warning messages by overriding automatic language defaults. You can do this by placing in the foreground those operators or functions designed to perform the behind-the-scenes operation. The example-warning message described in the preceding paragraph would be removed by doing an explicit cast of the argument from the type float to the type double.
Your First Unexpected Bug
The first error message, shown earlier in Figure 3-8, shows what might happen when you are using a new language for the first time. Here, the programmer tried to give a variable the name of a reserved, or language, keyword. If you are using a programming language that you are familiar with, you will probably not have this problem.
In C/C++, the word continue is a reserved, or language, keyword. In the sample program, the variable’s name was chosen for self-documenting, readability reasons; however, it bumped into a language restriction. Chapter 6 contains a table (Table 6-1) of these reserved words for you to refer to when initially creating your source code.
Viewing Output and Source Windows
Once you have viewed your list of warning and error messages, you will want to switch back to the edit window to make the necessary code changes. You can select the edit window by either clicking on the mouse inside the edit window itself or by going to the Window menu and clicking on the filename, ERROR.C. Using whichever approach you prefer (has the highlighter dried out yet?), make the edit window the topmost window.
Using Find and Replace
There will be times when you will want to quickly locate something within your program. You could do this by bringing down the Search | Replace... dialog box, but the Visual C++ IDE provides a quicker option. If you look closely at the toolbar in Figure 3-9, you will see the word “continue” in the Quick Find list box.
Figure 3-9: Using the Quick Find list box
To use Quick Find, simply click the left mouse button anywhere within the control’s interior and type the label you want to find. Quick Find can now be activated by pressing enter. Figure 3-9 shows the results of this action. The first occurrence of the continue variable is highlighted.
This approach is fine for locating first occurrences, but in our case it is inefficient because we need to locate all occurrences of the continue variable. For this reason, the Edit | Replace... dialog box, shown in Figure 3-10, is a better choice.
Figure 3-10: Using the editor’s Edit | Replace option
The easiest way to use Replace... is to first place the cursor on the word to search for before you invoke the Edit | Replace... option. If you follow this sequence, the word being searched for will be automatically entered into the Find what: list when you invoke the command.
In Figure 3-10, you saw this approach used by first placing the cursor on the variable continue, which was highlighted in the previous figure. Practice this sequence and see if you can get your screen to appear like the one in Figure 3-10.
For our sample program, we want the variable that is currently named continue to still be readable, but it needs to be spelled differently than the reserved word. At this point, you need to manually enter the word “continu” into the Replace dialog box’s Replace with: list.
Notice that this dialog box contains many of the standard word processor search-and-replace options, such as the ability to match whole words and designate case sensitivity. If you are new to the C/C++ language, you will be surprised to find out that C/C++ is case sensitive. For this reason, variables named TOTAL and total are treated as different variables.
One word of advice: Before you perform any search-and-replace operation, save the file. This will allow you to easily recover from a disastrous pattern match. Another approach is to use the Edit | Undo command. However, if your Undo buffer is not sufficiently large to hold all the changes the search-and-replace operation made, Undo might not be able to restore your whole program.
Now that you have entered the proper information into the Replace dialog box, you are ready to execute the replacement. However, there is one problem. The program contains the output statement “\nWould you like to continue (Y/N)”. If you were to choose the Replace dialog box option of Replace All, your output statement would have a spelling error in it, because a Replace All would misspell the word “continue” in the program’s screen output. For this reason, click now on the Find Next button.
Using Replace Options
The Replace dialog box presents you with several search options. Find Next searches for the search string’s next occurrence. Replace inserts the substitute string. The Replace All option races through your code without interruption, finding and replacing the targeted text.
In this example, you need to repeatedly choose Replace, followed by Find Next, until you have replaced every use of the variable continue with the new spelling, continu. Remember, do not change the spelling of the word “continue” in the printf( ) statement.
Shortcuts to Switching Views
Earlier you saw that switching between the output message window and the edit window required some keyboard or mouse gymnastics. There is an easier way to get these two windows to interact. But first, if you are following the example development cycle, you need to stop and rebuild your program. If you made all of the necessary continu substitutions described previously, your output message window should look like the one in Figure 3-11.
Figure 3-11: An updated output message window
The improved way to interact with these two windows is very straightforward. First, place the cursor on the warning or error message of interest. For our example, pick the first message in the new output message window:
warning C4013: ‘Printf’ undefined;...
Now press enter. Voil ! The integrated environment automatically switches to the edit window and automatically highlights the suspicious code segment (with an arrow), as shown in Figure 3-12.
Figure 3-12: Code containing errors is automatically highlighted with an arrow in the edit window
Useful Warning and Error Messages
When you learn a new language, you actually encounter two major learning curves. First, there’s the time it takes to learn the syntax and nuances of the new language itself. But the second, more subtle learning curve involves understanding this new environment’s help, warning, and error messages. In other words, you have to learn how this new compiler processes source code.
The good news is that the Visual C++ compiler produces some of the most accurate messages ever produced by any language environment. In our example, the compiler adroitly detected the misuse of a language keyword, continue.
As you now know, C/C++ is case sensitive. Once again, the compiler correctly detected an error. The function printf( ), supplied with your compiler, was defined in all lowercase letters. Because this function was accidentally entered with an uppercase “P”, the compiler was unable to locate a matching library function Printf( ). With this word highlighted in the edit window, make the edit change by replacing the uppercase “P” with its lowercase equivalent. Don’t forget to save your file.
More Work with the Debugger
At this point, the program is ready for another attempt at building an executable file. Return to the Project menu and select the Rebuild All menu item. Figure 3-13 shows the updated output messages.
Figure 3-13: Updated output messages for a second build of ERROR.C
Do you remember how to easily switch to the edit window and automatically locate the illegal escape sequence identified in the error message? (All you need to do is place the cursor on the error message and press enter.)
As it turns out, the same statement that contained the misspelled printf( ) function has a second error. In C/C++, all format strings must begin with a double quote. Edit the line by placing a double quote (“) after the opening parenthesis in the printf( ) function—that is, after printf(.
Make sure that your first printf( ) statement matches the one in Figure 3-14. Now, save the file and attempt another rebuild. Figure 3-14 shows the third updated output message window.
Figure 3-14: Updated output messages for a third of ERROR.C
Our last error message was
syntax error : missing ‘;’ before ‘)’
Place the cursor on the message and press enter. In the C/C++ language, unlike in Pascal, a semicolon is considered to be a statement terminator, not a statement separator. For this reason, the second statement within the for loop expression needs a terminating semicolon, not a comma. Change the comma after the constant SIZE to a semicolon, save the file, and execute a Rebuild All once again.
Success? According to the output message window, you should now have no warnings and no errors, and the Rebuild All command has successfully generated the executable file, ERROR.EXE.
  Note At this point, if your compiler’s errors and warnings message window does not show 0 errors and 1 warning (you can ignore this), you have inadvertently added a few typos of your own. Simply retrace your steps and edit the necessary code statements.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net