|
|
|
|
|
|
|
the right of the decimal point) than the output shown here. In the next chapter, we discuss how the programmer can control the appearance of floating point numbers in the output. |
|
|
|
|
|
|
|
|
Testing and Debugging Hints |
|
|
|
|
|
|
|
|
1. Every identifier that isn't a C++ reserved word must be declared. If you use a name that hasn't been declaredeither by your own declaration statements or by including a header fileyou get an error message. |
|
|
|
|
|
|
|
|
2. C++ is a case-sensitive language. Two identifiers that are capitalized differently are treated as two different identifiers. The word main and all C++ reserved words use only lowercase letters. |
|
|
|
|
|
|
|
|
3. An int constant other than 0 should not start with a zero. If it starts with zero, it is an octal (base-8) number. |
|
|
|
|
|
|
|
|
4. Watch out for integer division. The expression 47 / 100 yields 0, the integer quotient. This is one of the major sources of wrong output in C++ programs. |
|
|
|
|
|
|
|
|
5. Check for mismatched quotes in char constants and strings. Each char constant begins and ends with an apostrophe (single quote). Each string begins and ends with a double quote. |
|
|
|
|
|
|
|
|
6. Make sure your statements end in semicolons (except compound statements, which do not have a semicolon after the right brace). |
|
|
|
|
|
|
|
|
7. If the cause of an error in a program is not obvious, leave the computer and study a printed listing. Change your program only after you understand the source of the error. |
|
|
|
|
|
|
|
|
The syntax (grammar) of the C++ language is defined by a metalanguage. In this text, we use a form of metalanguage called syntax templates. We describe the semantics (meaning) of C++ statements in English. |
|
|
|
|
|
|
|
|
Identifiers are used in C++ to name things. Some identifiers, called reserved words, have predefined meanings in the language; others are created by the programmer. The identifiers you invent are restricted to those not reserved by the C++ language. Reserved words are listed in Appendix A. |
|
|
|
|
|
|
|
|
Identifiers are associated with memory locations by declarations. A declaration may give a name to a location whose value does not change (a constant) or to one whose value does change (a variable). Every constant and variable has an associated data type. C++ provides many predefined data types, the most common of which are int, float, and char. |
|
|
|
|
|
|
|
|
The assignment operator is used to change the value of a variable by as- |
|
|
|
|
|