2.2.1
Integers
You can assign a fixed value to a variable of type int using the = operator as follows.
int␣n u m e r O f P l a t o n i c S o l i d s␣=␣5;
When you type an integer you should not include any commas or spaces. So don’t write 1,000,000, simply write 1000000.
If you like you can specify an integer using a different numeric base. For example, if you prefer counting in hexadecimal (base 16) you can type 0xFF instead of 255. The prefix 0x means “This number is in hexadecimal”.
Confusingly, the prefix 0 on its own means “This number is in base 8”. So the code
doesn’t compile. Unfortunately, the chances of you remembering this fact are slim! Fortunately, the chances of you making this error are also reasonably low.
When you add two integers, you get an integer, of course. Similarly, if you multiply or subtract them. However. . .
Danger!
When you divide two integers in C++ you get another integer. C++ simply rounds down if necessary. The C++ code 3/5 evaluates to 0.
C++ actually gives you a number of choices for storing integer data de- pending on the potential range of values your variable might take. Here are some other data-type specifiers that are available, which all mean an integer of one form or another:
short int long long␣long unsigned␣short unsigned␣int unsigned␣long unsigned␣long␣long size_t
The range of values you can store in each of these data types varies between different C++ compilers. As you might guess, you can store smaller numbers in shorts than you can in long longs. What you can be sure of is that you will be able to store a value between −231 and 231− 1 in an int variable and a value between −263 and 263− 1 in a long␣long variable.
By specifying that a variable is unsigned you are saying that it is non- negative. Specifying the sign of a number takes up one bit of memory, so unsigned integers can be twice as big as signed ones.
If you add one to an integer that takes the maximum possible value for that data type, C++ wraps the value round so that it now takes the minimum possible value for that data type. In other words, C++ does its computation in binary and simply discards the top digits if an integer overflows the possible range.
This isn’t normally a problem in practice unless you decide to use the unsigned data types. For these data types you will find that the calculation 0 − 1 yields a number greater than 0. This is extremely confusing and leads to lots of bugs. For this reason I recommend that you avoid using the unsigned data types.
The type size_t is used to store the size of data structures stored in memory. Since the maximum amount of memory you can access on a 32-bit
computer is 32 bits long, a size_t is 32 bits long for 32-bit computers and 64 bits long for 64-bit computers1.
In summary: Use int or long␣long for most purposes; use size_t for the size of data structures; don’t bother with the other options.
2.2.2
Floating point numbers
Use double to store real numbers. Enter numerical values for real numbers by simply entering the decimal expansion with a . character for the decimal point.
Notice that C++ considers the numbers 1.0 and 1 to have different data types and so to be different.
If you want, you have a choice of data types for real numbers. You can use a float, a double, or a long␣double. We won’t describe the binary representation of real numbers in detail. However, it is worth knowing the following.
• Most real numbers can only be approximated by a computer. In com- puter code calculations using real numbers will contain small rounding errors. In particular, when testing your answers you should never test if two decimals are equal, only if they are approximately equal.
• The coding for decimals allows for some special double values: Inf, -Inf, NaN, -0.0. The first three represent, respectively, ∞, −∞, and not-a-number (for example, √−1 is not a real number). The -0.0 con- cept has no special purpose, but sometimes calculations give this as the answer. Since you should only ever check if two decimals are approxi- mately equal, the distinction shouldn’t cause problems.
2.2.3
Booleans
A bool is a variable which can take the value either true or false. These are special keywords in C++.
bool variables are called Boolean variables in polite company. They are named after the English mathematician George Boole.
When you print out a bool using cout␣<<, the value true is displayed as 1 and the value false is displayed as zero.
2.2.4
Characters
A char is a data type which is intended to store a character.
1For backwards compatibility, 64-bit computers can run 32-bit programs too. When they are running in 32-bit compatability mode, they behave just as though they were 32-bit computers.
Hex Character Hex Character Hex Character 20 space 40 60 ‘ 21 ! 41 A 61 a 22 " 42 B 62 b 23 # 43 C 63 c 24 $ 44 D 64 d 25 % 45 E 65 e 26 & 46 F 66 f 27 ’ 47 G 67 g 28 ( 48 H 68 h 29 ) 49 I 69 i 2A * 4A J 6A j 2B + 4B K 6B k 2C , 4C L 6C l 2D - 4D M 6D m 2E . 4E N 6E n 2F / 4F O 6F o 30 0 40 P 70 p 31 1 41 Q 71 q 32 2 42 R 72 r 33 3 43 S 73 s 34 4 44 T 74 t 35 5 45 U 75 u 36 6 46 V 76 v 37 7 47 W 77 w 38 8 48 X 78 x 39 9 49 Y 79 y 3A : 4A Z 7A z 3B ; 4B [ 7B { 3C < 4C \ 7C | 3D = 4D ] 7D } 3E > 4E ˆ 7E ˜ 3F ? 4F _ 7F delete
TABLE 2.1: ASCII mapping of hexadecimal character codes to visible char-
In memory a char is stored as a number between 0 and 255, or, in hexadec- imal, between 00 and FF. In other words, a char takes up exactly one byte. Some of the mappings between a numeric value and a character are given in Table 2.1.
Unfortunately, C++ was designed to be compatible with C, and C was written by Americans in the 1960s. The authors of C clearly thought that an alphabet of 256 characters would be more than enough for anyone, but for languages like Chinese it is nowhere near enough. Thus a char is an appropri- ate data type to store a character string written in English which may contain some numbers and punctuation marks. However, using the char data type is inappropriate for serious programs targeted at an international audience. We will largely ignore these issues in this book, since we are far more interested in working with numbers than working with text.
Interestingly, back when C was being devised, not everyone had computer screens. Some computers just printed their output on paper. For this reason the set of possible char values doesn’t just consist of letters, numbers, and punctuation, it also contains some instructions for a teleprinter. A teleprinter was a primitive printer that behaved more like a typewriter than a modern printer. As a result, there is a special char value ’\n’ which means the in- struction “new line”. ’\r’ means carriage return. ’\t’ means tab. There is even ’\a’ which means make a ping sound to alert the user that the printout is complete!
You specify a char by writing the character in quotes. For example:
char␣theLetterW␣=␣’W’;
Notice that a char represents a single character and not a sequence of char- acters.
If you are extremely astute you will want to know how you write the characters ’ and \, which seem to have special meanings. The answer is that you write \’ and \\. A standard piece of computer jargon is that \ is an escape character and the process of inserting \ characters is called escaping.
You can read and write a single characters using << and >> with cin and cout just as we have been doing for numbers.