📄 ch02.htm
字号:
type tells the compiler the size of the variable. For example, a <tt>char</tt> is 1 byte, and on modern computers an <tt>int</tt> is 4 bytes; thus, the variable <tt>round</tt> consumes four bytes (cubbyholes) of memory. </p><h4>Defining a Variable</h4><p>You define a variable by stating its type, followed by one or more spaces, the variable name, and a semicolon:</p><pre><tt>int round;</tt></pre><p>The variable name can be virtually any combination of letters, but it cannot contain spaces. Legal variable names include <tt>x</tt>, <tt>J23qrsnf</tt>, and <tt>myAge</tt>. It is good programming practice to use variable names that tell you what the variables are for. This makes them easier to understand, which makes it easier for you to maintain your program. </p><h4>Case Sensitivity</h4><p>C++ is case sensitive; therefore, a variable named <tt>round</tt> is different from <tt>Round</tt>, which is different from <tt>ROUND</tt>. Avoid using multiple variables whose names differ only by capitalization--it can be terribly confusing.</p><blockquote> <hr> <p><strong>NOTE: </strong> Some compilers enable you to turn case sensitivity off. Don't be tempted to do this. Your programs won't work with other compilers, and other C++ programmers will be very confused by your code. </p> <hr></blockquote><h4>Keywords</h4><p>C++ reserves some words, and you cannot use them as variable names. These are keywords that are used by the compiler to control your program. Keywords include <tt>if</tt>, <tt>while</tt>, <tt>for</tt>, and <tt>main</tt>. Your compiler manual probably provides a complete list, but generally, any reasonable name for a variable is almost certainly not a keyword. </p><p>Creating More Than One Variable at a Time</p><p>You can create more than one variable of the same type in one statement by writing the type and then the variable names, separated by commas. For example</p><pre><tt>int howManyLetters, howManyPositions;</tt><tt>bool valid, duplicatesAllowed; </tt></pre><h4>Assigning Values to Your Variables</h4><p>Back in listing 2.1, at line 36, a local variable is defined by stating the type (<tt>int</tt>) and the variable name (<tt>round</tt>). </p><p>This actually allocates memory for the variable. Because an <tt>int</tt> is four bytes, this allocates four bytes of memory. When the compiler allocates memory, it reserves the memory for the use of your variable and assigns the name that you provide (in this case, <tt>round</tt>). </p><h5>Scope</h5><p><i>Scope</i> refers to the region of a program in which an identifier--something that is named, such as an object, variable, function, or constant--is valid. When I say a variable has <i>local scope</i>, I mean that it is valid within a particular function. <a name="WhereWasI"></a></p><blockquote> <hr> <p> <b>Scope</b>--The region of a program in which an identifier (that is, the name of something) is valid.</p> <p> <b>Local scope</b>--When an identifier has local scope, it is valid within a particular function.</p> <hr></blockquote><p>There are other levels of scope (global, static member, and so on) that I will discuss as I progress through the program. </p><h5>The Value of Variables</h5><p>Local variables, such as <tt>round</tt>, have a value when they are created regardless of whether you initialize them. If you don't initialize them (as shown here), whatever happened to already be in the bit of memory is assigned to them--that is, a random <i>garbage</i> value. </p><p>It is good programming practice to <i>initialize</i> your variables. When you initialize a variable, you create it and give it a specific value, all in one step:</p><pre><tt>int round = 1;</tt></pre><p>This creates the variable <tt>round</tt> and initializes it with the value <tt>1</tt>.</p><p>Just as you can define more than one variable at a time, you can initialize more than one variable. For example,</p><pre><tt> int howManyLetters = 0, howManyPositions = 0;</tt></pre><p>initializes the two variables <tt>howManyLetters</tt>, each to the value <tt>0</tt>. You can even mix definitions and initializations:</p><pre><tt>int howManyLetters = 0, round, howManyPositions = 2;</tt></pre><p>This example defines three variables of type <tt>int</tt>, and it initializes the first and third. </p><h3> <a name="Heading8">Characters</a></h3><p>On line 46 of Listing 2.1, you created a character variable (type <tt>char</tt>) named <tt>choice</tt>. On most computers, character variables are 1 byte, enough to hold 256 values. A <tt>char</tt> can be interpreted as a small number (0-255) or as a member of the ASCII set. <i>ASCII</i> stands for the <i>American Standard Code for Information Interchange</i>. The ASCII character set and its <i>ISO</i> (<i>International Standards Organization</i>) equivalent are a way to encode all the letters, numerals, and punctuation marks.</p><blockquote> <hr> <p> <b>ASCII</b>--The American Standard Code for Information Interchange</p> <p> <b>ISO</b>--The International Standards Organization</p> <hr></blockquote><p>You create a character by placing the letter in single quotes. Therefore, <tt>'a'</tt> creates the character <i>a</i>.</p><p>In the ASCII code, the lowercase letter <i>a</i> is assigned the value <tt>97</tt>. All the lower- and uppercase letters, all the numerals, and all the punctuation marks are assigned values between <tt>1</tt> and <tt>128</tt>. Another 128 marks and symbols are reserved for use by the computer maker. </p><p>Characters and Numbers</p><p>When you insert a character--<tt>'a'</tt>, for example--into a <tt>char</tt> variable, what is really there is just a number between 0 and 255. The compiler knows, however, how to translate back and forth between characters and one of the ASCII values. </p><p>The value/letter relationship is arbitrary; there is no particular reason that the lowercase <i>a</i> is assigned the value <tt>97</tt>. As long as everyone (your keyboard, compiler, and screen) agrees, there is no problem. It is important to realize, however, that there is a big difference between the value <tt>5</tt> and the character <tt>'5'</tt>. The latter is actually valued at <tt>53</tt>, much as the letter <tt>'a'</tt> is valued at <tt>97</tt>.</p><p>Listing 2.2 is a simple program that prints the character values for the integers 32-127. Pay no attention to the details of this program--we will walk through how this works later in the book.</p><p><b>Listing 2.2 Printing out the Characters</b></p><pre><tt> #include <iostream ></tt><tt>using namespace std;</tt><tt>int main()</tt><tt> {</tt> for (int i = 32; i<128; i++) cout << (char) i; return 0; } !"#$%G'()*+,./0123456789:;<>?@ABCDEFGHIJKLMNOP_QRSTUVWXYZ[\]^'abcdefghijklmnopqrstuvwxyz<|>~s</pre><blockquote> <hr> <p><strong>NOTE: </strong> Your computer might print a slightly different list. </p> <hr></blockquote><h3> <a name="Heading9">Built-In Types</a></h3><p>C++ comes right out of the box with knowledge of a number of primitive built-in types. The type of a variable or object defines its size, its attributes, and its capabilities. </p><p>For example, an <tt>int</tt> is, on modern compilers, 4 bytes in size. It holds a value from <tt>-2,147,483,648</tt> to <tt>2,147,483,647</tt>. For more on bytes and why <tt>2,147,483,648</tt> is a round number, see Appendix A, "Binary and Hexadecimal."</p><p>You might think that an integer is an integer, but it isn't quite. The keyword <tt>integer</tt> refers to a four-byte value, but only if you are using a modern compiler on a modern 32-bit computer. If your software or computer is 16-bit, however, an integer might be only two bytes. The keyword <tt>short</tt> usually refers to a two-byte integer, and the keyword <tt>long</tt> most often refers to a four-byte integer, but neither of these is certain. The language requires only that a <tt>short</tt> is shorter than or equal to an integer, and an integer is shorter than or equal to a <tt>long</tt>. On my computer, a <tt>short</tt> is 2 bytes and an integer is 4, as is a <tt>long</tt>.</p><p>ISO C++ provides the types that are listed in Table 2.2.</p><p><b>Table 2.2 Variable Types </b></p><table border> <tr valign="TOP" align="left"> <td colspan=1 align="left"> <p><b>Type</b></p> </td> <td colspan=1 align="left"> <p><b>Size</b></p> </td> <td colspan=1 align="left"> <p><b>Values</b></p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"> <p>unsigned short <tt>int </tt></p> </td> <td colspan=1 align="left"> <p>2 bytes </p> </td> <td colspan=1 align="left"> <p><tt>0</tt> to <tt>65,535 </tt></p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"> <p>short <tt>int </tt></p> </td> <td colspan=1 align="left"> <p>2 bytes </p> </td> <td colspan=1 align="left"> <p><tt>-32,768</tt> to <tt>32,767 </tt></p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"> <p>unsigned long <tt>int </tt></p> </td> <td colspan=1 align="left"> <p>4 bytes </p> </td> <td colspan=1 align="left"> <p><tt>0</tt> to <tt>4,294,967,295 </tt></p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"> <p>long <tt>int </tt></p> </td> <td colspan=1 align="left"> <p>4 bytes </p> </td> <td colspan=1 align="left"> <p><tt>-2,147,483,648</tt> to <tt>2,147,483,647 </tt></p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"> <p><tt>int</tt> (16-bit) </p> </td> <td colspan=1 align="left"> <p>2 bytes </p> </td> <td colspan=1 align="left"> <p><tt>-32,768</tt> to <tt>32,767 </tt></p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"> <p><tt>int</tt> (32-bit) </p> </td> <td colspan=1 align="left"> <p>4 bytes </p> </td> <td colspan=1 align="left"> <p><tt>-2,147,483,648</tt> to <tt>2,147,483,647 </tt></p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"> <p>unsigned <tt>int</tt> (16-bit) </p> </td> <td colspan=1 align="left"> <p>2 bytes </p> </td> <td colspan=1 align="left"> <p><tt>0</tt> to <tt>65,535 </tt></p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"> <p>unsigned <tt>int</tt> (32-bit) </p> </td> <td colspan=1 align="left"> <p>4 bytes </p> </td> <td colspan=1 align="left"> <p><tt>0</tt> to <tt>4,294,967,295 </tt></p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"> <p><tt>char</tt></p> </td> <td colspan=1 align="left"> <p>1 byte </p> </td> <td colspan=1 align="left"> <p>256 character values </p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"> <p><tt>float</tt></p> </td> <td colspan=1 align="left"> <p>4 bytes </p> </td> <td colspan=1 align="left"> <p><tt>1.2e-38</tt> to <tt>3.4e38 </tt></p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"> <p><tt>double</tt></p> </td> <td colspan=1 align="left"> <p>8 bytes </p> </td> <td colspan=1 align="left"> <p><tt>2.2e-308</tt> to <tt>1.8e308</tt> </p> </td> </tr> <tr valign="TOP" align="left"> <td colspan=1 align="left"> <p><tt>bool</tt></p> </td> <td colspan=1 align="left"> <p>1 byte </p> </td> <td colspan=1 align="left"> <p><tt>true</tt> or <tt>false </tt></p> </td> </tr></table><blockquote> <hr> <p><strong>NOTE: </strong> ISO C++ recently added a new type, <tt>bool</tt>,<i> </i>which is a <tt>true</tt> or <tt>false</tt> value. <tt>bool</tt> is named after the British mathematician George Bool (1815-1864), who invented Boolean algebra, a system of symbolic logic. </p> <hr></blockquote><h4>Size of Integers</h4><p>This book assumes that you are using a 32-bit computer (for example, a Pentium) and that you are programming with a 32-bit compiler. With that development environment, an integer is always 4 bytes. Listing 2.3 can help you determine the size of the built-in types on your computer, using your compiler. </p><p><b>Listing 2.3 Finding the Size of Built-In Types</b></p><pre><tt>1: #include <iostream></tt><tt>2: using namespace std;</tt><tt>3: int main()</tt><tt>4: {</tt><tt>5: cout << "The size of an int is:\t\t";</tt><tt>5a: cout << sizeof(int) << " bytes.\n";</tt><tt>6: cout << "The size of a short int is:\t";</tt><tt>6a: cout << sizeof(short) << " bytes.\n";</tt><tt>7: cout << "The size of a long int is:\t";</tt><tt>7a: cout << sizeof(long) << " bytes.\n";</tt>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -