< previous page page_582 next page >

Page 582
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Also, if you accidentally omit the parentheses around the assignment, like this:
if (x = y < z)
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
then, according to C++ operator precedence, x is not assigned the value of y. It is assigned either the value 1 (if y<z) or 0 (if y>z).
2. Programs that rely on a particular machine's character set may not run on another machine. Check to see what character-handling functions are supplied by the standard library. Functions like tolower, toupper, isalpha, and iscntrl automatically account for the character set being used.
3. Don't directly compare floating point values for equality. Instead, check them for near equality. The tolerance for near equality depends on the particular problem you are solving.
4. Use integers if you are dealing with whole numbers only. Any integer can be represented exactly by the computer, as long as it is within the machine's allowable range of values. Also, integer arithmetic is faster than floating point arithmetic on most machines.
5. Be aware of representational, cancellation, overflow, and underflow errors. If possible, try to arrange calculations in your program to keep floating point numbers from becoming too large or too small.
6. If your program increases the value of a positive integer and the result suddenly becomes a negative number, you should suspect integer overflow. On most computers, adding 1 to INT_MAX yields INT_MIN, a negative number.
7. Except when you really need to, avoid mixing data types in expressions, assignment operations, parameter passage, and the return of a function value. If you must mix types, explicit type casts can prevent unwelcome surprises caused by implicit type coercion.
8. Consider using enumeration types to make your programs more readable, understandable, and modifiable.
9. Avoid anonymous data typing. Give each user-defined type a name.
10. Enumeration type values cannot be input or output directly.
11. Type demotion can lead to decreased precision or corruption of data.
Summary
A data type is a set of values (domain) along with the operations that can be applied to those values. Simple data types are data types whose values are atomic (indivisible).

 
< previous page page_582 next page >