|
|
|
|
|
|
|
In most cases, a minus sign preceding an integer value makes the integer negative: |
|
|
|
|
|
|
|
|
The exception is when you explicitly add the reserved word unsigned to the data type name: |
|
|
|
|
|
|
|
|
An unsigned integer value is assumed to be only positive or zero. It is only for very specialized, advanced problems that you will need to use unsigned types. We rarely use unsigned in this book. |
|
|
|
|
|
|
|
|
The data types char, short, int, and long are intended to represent different sizes of integers, from smaller (fewer bits) to larger (more bits). |
|
|
|
|
|
|
|
|
char memory cell  |
|
|
|
|
|
|
|
|
short memory cell  |
|
|
|
|
|
|
|
|
int memory cell  |
|
|
|
|
|
|
|
|
long memory cell  |
|
|
|
|
|
|
|
|
The sizes are machine dependent (that is, they may vary from machine to machine). In general, the more bits there are in the memory cell, the larger the integer value that can be stored. |
|
|
|
|
|
|
|
|
int is by far the most common data type for manipulating integer data. In the Payroll program, the identifier for the employee number, empNum, is of data type int. You nearly always use int for manipulating integer values, but sometimes you have to use long if your program requires values larger than the maximum int value. (On many personal computers, the range of int values is from -32768 through +32767. On larger machines, ints often range from -2147483648 through +2147483647.) If your program tries to compute a value larger than your machine's maximum value, the result is integer overflow. Some machines give you an error message when overflow occurs, but others don't. We talk more about overflow in later chapters. |
|
|
|
|
|
|
|
|
One caution about integer values in C++: A value beginning with a zero is taken to be an octal (base-8) number instead of a decimal (base-10) number. If you write |
|
|
|
|
|