📄 ch03.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><!-- This document was created from RTF source by rtftohtml version 3.0.1 --> <META NAME="GENERATOR" Content="Symantec Visual Page 1.0"> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1"> <TITLE>Teach Yourself C++ in 21 Days</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><H1 ALIGN="CENTER"><A HREF="ch02.htm"><IMG SRC="../buttons/BLANPREV.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="http://www.mcp.com/sams"><IMGSRC="../buttons/BLANHOME.GIF" WIDTH="37" HEIGHT="37" ALIGN="BOTTOM"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../buttons/BLANTOC.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="ch04.htm"><IMG SRC="../buttons/BLANNEXT.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A></H1><H1></H1><UL> <LI><A HREF="#Heading1">Day 3</A> <UL> <LI><A HREF="#Heading2">Variables and Constants</A> <UL> <LI><A HREF="#Heading3">What Is a Variable?</A> <UL> <LI><A HREF="#Heading4">Figure 3.1.</A> </UL> <LI><A HREF="#Heading5">Setting Aside Memory</A> <UL> <LI><A HREF="#Heading6">Size of Integers</A> </UL> <LI><A HREF="#Heading7">Listing 3.1. Determining the size of variable types</A> <LI><A HREF="#Heading8">on your computer.</A> <UL> <LI><A HREF="#Heading9">signed and unsigned</A> <LI><A HREF="#Heading10">Fundamental Variable Types</A> </UL> <LI><A HREF="#Heading11">Defining a Variable</A> <UL> <LI><A HREF="#Heading12">Case Sensitivity</A> <LI><A HREF="#Heading13">Keywords</A> </UL> <LI><A HREF="#Heading14">Creating More Than One Variable at a Time</A> <LI><A HREF="#Heading16">Assigning Values to Your Variables</A> <LI><A HREF="#Heading17">Listing 3.2. A demonstration of the use of variables</A><A HREF="#Heading18">.</A> <LI><A HREF="#Heading19">typedef</A> <LI><A HREF="#Heading20">Listing 3.3. A demonstration of typedef</A><A HREF="#Heading21">.</A> <LI><A HREF="#Heading22">When to Use short and When to Use long</A> <UL> <LI><A HREF="#Heading23">Wrapping Around an unsigned Integer</A> </UL> <LI><A HREF="#Heading24">Listing 3.4.</A> <LI><A HREF="#Heading25">A demonstration of putting too large a value in an unsigned integer.</A> <UL> <LI><A HREF="#Heading26">Wrapping Around a signed Integer</A> </UL> <LI><A HREF="#Heading27">Listing 3.5.</A> <LI><A HREF="#Heading28">A demonstration of adding too large a number to a signed integer.</A> <LI><A HREF="#Heading29">Characters</A> <UL> <LI><A HREF="#Heading30">Characters and Numbers</A> </UL> <LI><A HREF="#Heading31">Listing 3.6. Printing characters based on numbers</A><A HREF="#Heading32">.</A> <UL> <LI><A HREF="#Heading33">Special Printing Characters</A> </UL> <LI><A HREF="#Heading34">Constants</A> <UL> <LI><A HREF="#Heading35">Literal Constants</A> <LI><A HREF="#Heading36">Symbolic Constants</A> </UL> <LI><A HREF="#Heading37">Enumerated Constants</A> <LI><A HREF="#Heading38">Listing 3.7. A demonstration of enumerated constants</A> <LI><A HREF="#Heading39">.</A> <LI><A HREF="#Heading40">Summary</A> <LI><A HREF="#Heading41">Q&A</A> <LI><A HREF="#Heading42">Workshop</A> <UL> <LI><A HREF="#Heading43">Quiz</A> <LI><A HREF="#Heading44">Exercises</A> </UL> </UL> </UL></UL><P><HR SIZE="4"><H2 ALIGN="CENTER"><A NAME="Heading1"></A><FONT COLOR="#000077">Day 3</FONT></H2><H2 ALIGN="CENTER"><A NAME="Heading2"></A><FONT COLOR="#000077">Variables and Constants</FONT></H2><P>Programs need a way to store the data they use. Variables and constants offervarious ways to represent and manipulate that data.</P><P>Today you will learn<UL> <LI>How to declare and define variables and constants. <P> <LI>How to assign values to variables and manipulate those values. <P> <LI>How to write the value of a variable to the screen.</UL><H3 ALIGN="CENTER"><A NAME="Heading3"></A><FONT COLOR="#000077">What Is a Variable?</FONT></H3><P>In C++ a variable is a place to store information. A variable is a location inyour computer's memory in which you can store a value and from which you can laterretrieve that value.</P><P>Your computer's memory can be viewed as a series of cubbyholes. Each cubbyholeis one of many, many such holes all lined up. Each cubbyhole--or memory location--isnumbered sequentially. These numbers are known as memory addresses. A variable reservesone or more cubbyholes in which you may store a value.</P><P>Your variable's name (for example, <TT>myVariable</TT>) is a label on one of thesecubbyholes, so that you can find it easily without knowing its actual memory address.Figure 3.1 is a schematic representation of this idea. As you can see from the figure,<TT>myVariable</TT> starts at memory address <TT>103</TT>. Depending on the sizeof <TT>myVariable</TT>, it can take up one or more memory addresses.<BR><BR><A NAME="Heading4"></A><A HREF="../art/ch03/032cp01.jpg"><FONT COLOR="#000077">Figure3.1.</FONT></A><FONT COLOR="#000077"> </FONT><I>A schematic representation of memory.</I><BLOCKQUOTE> <P><HR><FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>RAM is random access memory. When you run your program, it is loaded into RAM from the disk file. All variables are also created in RAM. When programmers talk of memory, it is usually RAM to which they are referring. <HR></BLOCKQUOTE><H4 ALIGN="CENTER"><A NAME="Heading5"></A><FONT COLOR="#000077">Setting Aside Memory</FONT></H4><P>When you define a variable in C++, you must tell the compiler what kind of variableit is: an integer, a character, and so forth. This information tells the compilerhow much room to set aside and what kind of value you want to store in your variable.</P><P>Each cubbyhole is one byte large. If the type of variable you create is two bytesin size, it needs two bytes of memory, or two cubbyholes. The type of the variable(for example, integer) tells the compiler how much memory (how many cubbyholes) toset aside for the variable.</P><P>Because computers use bits and bytes to represent values, and because memory ismeasured in bytes, it is important that you understand and are comfortable with theseconcepts. For a full review of this topic, please read Appendix B, "C++ Keywords."<H4 ALIGN="CENTER"><A NAME="Heading6"></A><FONT COLOR="#000077">Size of Integers</FONT></H4><P>On any one computer, each variable type takes up a single, unchanging amount ofroom. That is, an integer might be two bytes on one machine, and four on another,but on either computer it is always the same, day in and day out.</P><P>A <TT>char</TT> variable (used to hold characters) is most often one byte long.A <TT>short</TT> integer is two bytes on most computers, a <TT>long</TT> integeris usually four bytes, and an integer (without the keyword <TT>short</TT> or <TT>long</TT>)can be two or four bytes. Listing 3.1 should help you determine the exact size ofthese types on your computer.</P><DL> <DD><HR><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>character</I> is a single letter, number, or symbol that takes up one byte of memory. <HR></DL><P><A NAME="Heading7"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 3.1. Determiningthe size of variable types on your computer.</B></FONT></P><PRE><FONT COLOR="#0066FF">1: #include <iostream.h>2:3: int main()4: {5: cout << "The size of an int is:\t\t" << sizeof(int) << " bytes.\n";6: cout << "The size of a short int is:\t" << sizeof(short) << " bytes.\n";7: cout << "The size of a long int is:\t" << sizeof(long) << " bytes.\n";8: cout << "The size of a char is:\t\t" << sizeof(char) << " bytes.\n";9: cout << "The size of a float is:\t\t" << sizeof(float) << " bytes.\n";10: cout << "The size of a double is:\t" << sizeof(double) << " bytes.\n";11:12: return 0;<TT>13: }</TT>Output: The size of an int is: 2 bytes.The size of a short int is: 2 bytes.The size of a long int is: 4 bytes.The size of a char is: 1 bytes.The size of a float is: 4 bytes.The size of a double is: 8 bytes.</FONT></PRE><BLOCKQUOTE> <P><HR><FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>On your computer, the number of bytes presented might be different. <HR></BLOCKQUOTE><DL> <DD><HR><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>Most of Listing 3.1 should be pretty familiar. The one new feature is the use of the <TT>sizeof()</TT> function in lines 5 through 10. <TT>sizeof()</TT> is provided by your compiler, and it tells you the size of the object you pass in as a parameter. For example, on line 5 the keyword <TT>int</TT> is passed into <TT>sizeof()</TT>. Using <TT>sizeof()</TT>, I was able to determine that on my computer an <TT>int</TT> is equal to a <TT>short</TT> <TT>int</TT>, which is 2 bytes. <HR></DL><H4 ALIGN="CENTER"><A NAME="Heading9"></A><FONT COLOR="#000077">signed and unsigned</FONT></H4><P>In addition, all integer types come in two varieties: <TT>signed</TT> and <TT>unsigned</TT>.The idea here is that sometimes you need negative numbers, and sometimes you don't.Integers (<TT>short</TT> and <TT>long</TT>) without the word "unsigned"are assumed to be <TT>signed</TT>. <TT>Signed</TT> integers are either negative orpositive. <TT>Unsigned</TT> integers are always positive.</P><P>Because you have the same number of bytes for both <TT>signed</TT> and <TT>unsigned</TT>integers, the largest number you can store in an <TT>unsigned</TT> integer is twiceas big as the largest positive number you can store in a <TT>signed</TT> integer.An <TT>unsigned</TT> <TT>short</TT> integer can handle numbers from 0 to 65,535.Half the numbers represented by a <TT>signed</TT> <TT>short</TT> are negative, thusa <TT>signed</TT> <TT>short</TT> can only represent numbers from -32,768 to 32,767.If this is confusing, be sure to read Appendix A, "Operator Precedence."<H4 ALIGN="CENTER"><A NAME="Heading10"></A><FONT COLOR="#000077">Fundamental VariableTypes</FONT></H4><P>Several other variable types are built into C++. They can be conveniently dividedinto integer variables (the type discussed so far), floating-point variables, andcharacter variables.</P><P>Floating-point variables have values that can be expressed as fractions--thatis, they are real numbers. Character variables hold a single byte and are used forholding the 256 characters and symbols of the ASCII and extended ASCII charactersets.</P><DL> <DD><HR><FONT COLOR="#000077"><B>New Term: </B></FONT><I>The ASCII character set</I> is the set of characters standardized for use on computers. ASCII is an acronym for American Standard Code for Information Interchange. Nearly every computer operating system supports ASCII, though many support other international character sets as well. <HR></DL><P>The types of variables used in C++ programs are described in Table 3.1. This tableshows the variable type, how much room this book assumes it takes in memory, andwhat kinds of values can be stored in these variables. The values that can be storedare determined by the size of the variable types, so check your output from Listing3.1. <BR><BR><FONT SIZE="4"><B>Table 3.1. Variable Types. </B></FONT><TABLE BORDER="0"> <TR ALIGN="LEFT" rowspan="1"> <TD WIDTH="139" ALIGN="LEFT"><B><I>Type</I></B></TD> <TD WIDTH="67" ALIGN="LEFT"><B><I>Size</I></B></TD> <TD ALIGN="LEFT"><B><I>Values</I></B></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD WIDTH="139" ALIGN="LEFT"><TT>unsigned short int</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>short int</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>unsigned long int</TT></TD> <TD WIDTH="67" ALIGN="LEFT">4 bytes</TD> <TD ALIGN="LEFT">0 to 4,294,967,295</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD WIDTH="139" ALIGN="LEFT"><TT>long int</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">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -