📄 ch03.htm
字号:
<TD WIDTH="139" ALIGN="LEFT"><TT>int (16 bit)</TT></TD>
<TD WIDTH="67" ALIGN="LEFT">2 bytes</TD>
<TD ALIGN="LEFT">-32,768 to 32,767</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="139" ALIGN="LEFT"><TT>int (32 bit)</TT></TD>
<TD WIDTH="67" ALIGN="LEFT">4 bytes</TD>
<TD ALIGN="LEFT">-2,147,483,648 to 2,147,483,647</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="139" ALIGN="LEFT"><TT>unsigned int (16 bit)</TT></TD>
<TD WIDTH="67" ALIGN="LEFT">2 bytes</TD>
<TD ALIGN="LEFT">0 to 65,535</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="139" ALIGN="LEFT"><TT>unsigned int (32 bit)</TT></TD>
<TD WIDTH="67" ALIGN="LEFT">2 bytes</TD>
<TD ALIGN="LEFT">0 to 4,294,967,295</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="139" ALIGN="LEFT"><TT>char</TT></TD>
<TD WIDTH="67" ALIGN="LEFT">1 byte</TD>
<TD ALIGN="LEFT">256 character values</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="139" ALIGN="LEFT"><TT>float</TT></TD>
<TD WIDTH="67" ALIGN="LEFT">4 bytes</TD>
<TD ALIGN="LEFT">1.2e-38 to 3.4e38</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="139" ALIGN="LEFT"><TT>double</TT></TD>
<TD WIDTH="67" ALIGN="LEFT">8 bytes</TD>
<TD ALIGN="LEFT">2.2e-308 to 1.8e308</TD>
</TR>
</TABLE>
<BLOCKQUOTE>
<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>The sizes of variables might be
different from those shown in Table 3.1, depending on the compiler and the computer
you are using. If your computer had the same output as was presented in Listing 3.1,
Table 3.1 should apply to your compiler. If your output from Listing 3.1 was different,
you should consult your compiler's manual for the values that your variable types
can hold.
<HR>
</BLOCKQUOTE>
<CENTER>
<H3><A NAME="Heading11"></A><FONT COLOR="#000077">Defining a Variable</FONT></H3>
</CENTER>
<P>You create or define a variable by stating its type, followed by one or more spaces,
followed by the variable name and a semicolon. The variable name can be virtually
any combination of letters, but cannot contain spaces. Legal variable names include
<TT>x</TT>, <TT>J23qrsnf</TT>, and <TT>myAge</TT>. Good variable names tell you what
the variables are for; using good names makes it easier to understand the flow of
your program. The following statement defines an integer variable called <TT>myAge</TT>:</P>
<PRE><FONT COLOR="#0066FF">int myAge;
</FONT></PRE>
<P>As a general programming practice, avoid such horrific names as <TT>J23qrsnf</TT>,
and restrict single-letter variable names (such as <TT>x</TT> or <TT>i</TT>) to variables
that are used only very briefly. Try to use expressive names such as <TT>myAge</TT>
or <TT>howMany</TT>. Such names are easier to understand three weeks later when you
are scratching your head trying to figure out what you meant when you wrote that
line of code.</P>
<P>Try this experiment: Guess what these pieces of programs do, based on the first
few lines of code:</P>
<P>Example 1</P>
<PRE><FONT COLOR="#0066FF">main()
{
unsigned short x;
unsigned short y;
ULONG z;
z = x * y;
}
</FONT></PRE>
<P>Example 2</P>
<PRE><FONT COLOR="#0066FF">main ()
{
unsigned short Width;
unsigned short Length;
unsigned short Area;
Area = Width * Length;
}
</FONT></PRE>
<P>Clearly, the second program is easier to understand, and the inconvenience of
having to type the longer variable names is more than made up for by how much easier
it is to maintain the second program.
<CENTER>
<H4><A NAME="Heading12"></A><FONT COLOR="#000077">Case Sensitivity</FONT></H4>
</CENTER>
<P>C++ is case-sensitive. In other words, uppercase and lowercase letters are considered
to be different. A variable named <TT>age</TT> is different from <TT>Age</TT>, which
is different from <TT>AGE</TT>.
<BLOCKQUOTE>
<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>Some compilers allow 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.
<HR>
</BLOCKQUOTE>
<P>There are various conventions for how to name variables, and although it doesn't
much matter which method you adopt, it is important to be consistent throughout your
program.</P>
<P>Many programmers prefer to use all lowercase letters for their variable names.
If the name requires two words (for example, my car), there are two popular conventions:
<TT>my_car</TT> or <TT>myCar</TT>. The latter form is called camel-notation, because
the capitalization looks something like a camel's hump.</P>
<P>Some people find the underscore character (<TT>my_car</TT>) to be easier to read,
while others prefer to avoid the underscore, because it is more difficult to type.
This book uses camel-notation, in which the second and all subsequent words are capitalized:
<TT>myCar</TT>, <TT>theQuickBrownFox</TT>, and so forth.
<BLOCKQUOTE>
<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>Many advanced programmers employ
a notation style that is often referred to as Hungarian notation. The idea behind
Hungarian notation is to prefix every variable with a set of characters that describes
its type. Integer variables might begin with a lowercase letter i, longs might begin
with a lowercase l. Other notations indicate constants, globals, pointers, and so
forth. Most of this is much more important in C programming, because C++ supports
the creation of user-defined types (see Day 6, "Basic Classes") and because
C++ is strongly typed.
<HR>
</BLOCKQUOTE>
<CENTER>
<H4><A NAME="Heading13"></A><FONT COLOR="#000077">Keywords</FONT></H4>
</CENTER>
<P>Some words are reserved by C++, and you may not use them as variable names. These
are keywords 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 should provide
a complete list, but generally, any reasonable name for a variable is almost certainly
not a keyword.
<BLOCKQUOTE>
<P>
<HR>
<B>DO</B> define a variable by writing the type, then the variable name. <B>DO</B>
use meaningful variable names. DO remember that C++ is case sensitive. <B>DON'T</B>
use C++ keywords as variable names. DO understand the number of bytes each variable
type consumes in memory, and what values can be stored in variables of that type.
<B>DON'T</B> use <TT>unsigned</TT> variables for negative numbers.
<HR>
</BLOCKQUOTE>
<CENTER>
<H3><A NAME="Heading14"></A><FONT COLOR="#000077">Creating More Than One Variable
at a Time</FONT></H3>
</CENTER>
<CENTER>
<H3><FONT COLOR="#000077"></FONT></H3>
</CENTER>
<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><FONT COLOR="#0066FF">unsigned int myAge, myWeight; // two unsigned int variables
long area, width, length; // three longs
</FONT></PRE>
<P>As you can see, <TT>myAge</TT> and <TT>myWeight</TT> are each declared as <TT>unsigned</TT>
integer variables. The second line declares three individual <TT>long</TT> variables
named <TT>area</TT>, <TT>width</TT>, and <TT>length</TT>. The type (<TT>long</TT>)
is assigned to all the variables, so you cannot mix types in one definition statement.
<CENTER>
<H3><A NAME="Heading16"></A><FONT COLOR="#000077">Assigning Values to Your Variables</FONT></H3>
</CENTER>
<P>You assign a value to a variable by using the assignment operator (<TT>=</TT>).
Thus, you would assign <TT>5</TT> to <TT>Width</TT> by writing</P>
<PRE><FONT COLOR="#0066FF">unsigned short Width;
Width = 5;
</FONT></PRE>
<P>You can combine these steps and initialize <TT>Width</TT> when you define it by
writing</P>
<PRE><FONT COLOR="#0066FF">unsigned short Width = 5;
</FONT></PRE>
<P>Initialization looks very much like assignment, and with integer variables, the
difference is minor. Later, when constants are covered, you will see that some values
must be initialized because they cannot be assigned to. The essential difference
is that initialization takes place at the moment you create the variable.</P>
<P>Just as you can define more than one variable at a time, you can initialize more
than one variable at creation. For example:</P>
<PRE><FONT COLOR="#0066FF">// create two long variables and initialize them
Âlong width = 5, length = 7;
</FONT></PRE>
<P>This example initializes the <TT>long</TT> integer variable <TT>width</TT> to
the value <TT>5</TT> and the <TT>long</TT> integer variable <TT>length</TT> to the
value <TT>7</TT>. You can even mix definitions and initializations:</P>
<PRE><FONT COLOR="#0066FF">int myAge = 39, yourAge, hisAge = 40;
</FONT></PRE>
<P>This example creates three type <TT>int</TT> variables, and it initializes the
first and third.</P>
<P>Listing 3.2 shows a complete program, ready to compile, that computes the area
of a rectangle and writes the answer to the screen.</P>
<P><A NAME="Heading17"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 3.2. A demonstration
of the use of variables.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">1: // Demonstration of variables
2: #include <iostream.h>
3:
4: int main()
5: {
6: unsigned short int Width = 5, Length;
7: Length = 10;
8:
9: // create an unsigned short and initialize with result
10: // of multiplying Width by Length
11: unsigned short int Area = Width * Length;
12:
13: cout << "Width:" << Width << "\n";
14: cout << "Length: " << Length << endl;
15: cout << "Area: " << Area << endl;
16: return 0;
<TT>17: }</TT></FONT></PRE>
<PRE><FONT COLOR="#0066FF">Output: Width:5
Length: 10
Area: 50
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>Line 2 includes the required
<TT>include</TT> statement for the <TT>iostream</TT>'s library so that <TT>cout</TT>
will work. Line 4 begins the program. <BR>
<BR>
On line 6, <TT>Width</TT> is defined as an <TT>unsigned</TT> <TT>short</TT> integer,
and its value is initialized to <TT>5</TT>. Another <TT>unsigned short</TT> integer,
<TT>Length</TT>, is also defined, but it is not initialized. On line 7, the value
<TT>10</TT> is assigned to <TT>Length</TT>.</P>
<P>On line 11, an <TT>unsigned short</TT> integer, <TT>Area</TT>, is defined, and
it is initialized with the value obtained by multiplying <TT>Width</TT> times <TT>Length</TT>.
On lines 13-15, the values of the variables are printed to the screen. Note that
the special word <TT>endl</TT> creates a new line.
<CENTER>
<H3><A NAME="Heading19"></A><FONT COLOR="#000077">typedef</FONT></H3>
</CENTER>
<P>It can become tedious, repetitious, and, most important, error-prone to keep writing
<TT>unsigned short int</TT>. C++ enables you to create an alias for this phrase by
using the keyword <TT>typedef</TT>, which stands for type definition.</P>
<P>In effect, you are creating a synonym, and it is important to distinguish this
from creating a new type (which you will do on Day 6). <TT>typedef</TT> is used by
writing the keyword <TT>typedef</TT>, followed by the existing type and then the
new name. For example</P>
<PRE><FONT COLOR="#0066FF">typedef unsigned short int USHORT
</FONT></PRE>
<P>creates the new name <TT>USHORT</TT> that you can use anywhere you might have
written <TT>unsigned short int</TT>. Listing 3.3 is a replay of Listing 3.2, using
the type definition <TT>USHORT</TT> rather than <TT>unsigned short int</TT>.</P>
<P><A NAME="Heading20"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 3.3. A demonstration
of typedef</B></FONT><FONT SIZE="2" COLOR="#000077"><B>.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">1: // *****************
2: // Demonstrates typedef keyword
3: #include <iostream.h>
4:
5: typedef unsigned short int USHORT; //typedef defined
6:
7: void main()
8: {
9: USHORT Width = 5;
10: USHORT Length;
11: Length = 10;
12: USHORT Area = Width * Length;
13: cout << "Width:" << Width << "\n";
14: cout << "Length: " << Length << endl;
15: cout << "Area: " << Area <<endl;
<TT>16: }</TT>
Output: Width:5
Length: 10
Area: 50
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis: </B></FONT>On line 5, <TT>USHORT</TT> is typedefined
as a synonym for <TT>unsigned short int</TT>. The program is very much like Listing
3.2, and the output is the same.
<CENTER>
<H3><A NAME="Heading22"></A><FONT COLOR="#000077">When to Use short and When to Use
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -