⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tut1-2.html

📁 a Complete C++ language tutorial on the cplusplus.com
💻 HTML
📖 第 1 页 / 共 2 页
字号:

<P>
Do not worry if something about the variable declarations looks a bit strange to you.
You will see the rest in detail in coming sections.

<P>
<H2>Initialization of variables</H2>
When declaring a local variable, its value is undetermined by default.
But you may want a variable to store a concrete value the moment that it is declared.
In order to do that, you have to append an equal sign followed by the value wanted to the
variable declaration:
<BLOCKQUOTE><TT>
<I>type identifier</I> <B>=</B> <I>initial_value</I> ;
</TT></BLOCKQUOTE>
For example, if we want to declare an <TT><B>int</B></TT> variable called <TT><B>a</B></TT>
that contains the value <TT><B>0</B></TT> at the moment in which it is declared, we could
write:
<BLOCKQUOTE><TT>int a = 0;</TT></BLOCKQUOTE>
<P>
Additionally to this way of initializating variables (known as c-like), C++ has added a new
way to initialize a variable: by enclosing the initial value between parenthesis
<TT><B>()</B></TT>:
<BLOCKQUOTE><TT>
<I>type identifier</I> <B>(</B><I>initial_value</I><B>)</B> ;
</TT></BLOCKQUOTE>
For example:
<BLOCKQUOTE><TT>
int a (0);
</TT></BLOCKQUOTE>
Both ways are valid and equivalent in C++.

<p>
<H2>Scope of variables</H2>

<IMG SRC="icoc-cpp.gif" ALIGN="left">
All the variables that we are going to use must have been previously declared.
An important difference between the C and C++ languages, is that in C++ we can declare
variables anywhere in the source code, even between two executable sentences, and not only
at the beginning of a block of instructions, like happens in C.

<P>
Anyway, it is recommended under some circumstances to follow the indications of the
C language when declaring variables, since it can be useful when debugging
a program to have all the declarations grouped together.  Therefore, the traditional C-like
way to declare variables is to include their declaration at the beginning of each
function (for local variables) or directly in the body of the program
outside any function (for global variables).
<p>
<table><tr><td>
<img src="imgvars1.gif" align="left">
<b>Global variables</b> can be referred to anywhere in the code, within any function,
whenever it is after its declaration.
<p>
The scope of the <b>local variables</b> is limited to the code level in which they are
declared. If they are declared at the beginning of a function
(like in <TT><B>main</B></TT>) their
scope is the whole <TT><B>main</B></TT> function.  In the example above, this means that
if another function existed in addition to <tt>main()</tt>, the local variables
declared in <TT><B>main</B></TT> could not be used in the other function and vice versa.
<P>
In C++, the scope of a local variable is given by the block in which it is declared
(a block is a group of instructions grouped together within curly brackets <TT><B>{}</B></TT>
signs). If it is declared within a function it will be a variable with function scope,
if it is declared in a loop its scope will be only the loop, etc...
<p>
In addition to <B>local</B> and <B>global</B> scopes there exists external scope, that
causes a variable to be visible not only in the same source file but in all other files
that will be linked together.
</td></tr></table>


<p>
<h2>Constants:  Literals.</h2>
A constant is any expression that has a fixed value. They can be divided in
Integer Numbers, Floating-Point Numbers, Characters and Strings.
<P>
<b>Integer Numbers</b><br>
<blockquote><tt>
1776<br>707<br>-273<br>
</tt></blockquote>
they are numerical constants that identify integer decimal numbers. Notice that to express
a numerical constant we do not need to write quotes (<tt><b>"</b></tt>) nor any special
character. There is no doubt that it is a constant: whenever we write <tt><b>1776</b></tt>
in a program we will be referring to the value 1776.
<p>
In addition to decimal numbers (those that all of us already know) C++ allows the use
as literal constants of octal numbers (base 8) and hexadecimal numbers (base 16).
If we want to express an octal number we must precede it with a
<tt><b>0</b></tt> character (zero character). And to express a hexadecimal number we have
to precede it with the characters <tt><b>0x</b></tt> (zero, x).  For example, the following literal
constants are all equivalent to each other:
<blockquote><tt><PRE>
75         <FONT COLOR="green"><I>// decimal</I></FONT>
0113       <FONT COLOR="green"><I>// octal</I></FONT>
0x4b       <FONT COLOR="green"><I>// hexadecimal</I></FONT>
</PRE></TT></blockquote>
All of them represent the same number: 75 (seventy five) expressed as a radix-10 number,
octal and hexdecimal, respectively.
<p>
[ Note:  You can find more information on hexadecimal and octal representations in the
document <a href="../papers/hex.html">Numerical radixes</a>]
<p>
<b>Floating Point Numbers</b><br>
They express numbers with decimals and/or exponents. They can include a decimal point, an
<tt><b>e</b></tt> character (that expresses "by ten at the Xth height", where X is the
following integer value) or both.
<blockquote><tt><PRE>
3.14159    <FONT COLOR="green"><I>// 3.14159</I></FONT>
6.02e23    <FONT COLOR="green"><I>// 6.02 x 10<SUP><SMALL>23</SMALL></SUP></I></FONT>
1.6e-19    <FONT COLOR="green"><I>// 1.6 x 10<SUP><SMALL>-19</SMALL></SUP></I></FONT>
3.0        <FONT COLOR="green"><I>// 3.0</I></FONT>
</PRE></tt></blockquote>
these are four valid numbers with decimals expressed in C++.
The first number is PI, the second one is the number of Avogadro, the third is the electric
charge of an electron (an extremely small number) -all of them approximated- and the last
one is the number <tt>3</tt> expressed as a floating point numeric literal.
<p>
<b>Characters and strings</b><br>
There also exist non-numerical constants, like:
<blockquote><tt>
'z'<br>'p'<br>"Hello world"<br>"How do you do?"
</tt></blockquote>

The first two expressions represent single characters,
and the following two represent strings of several characters.
Notice that to represent a single character we enclose it between single quotes (<tt>'</tt>)
and to express a string of more than one character we enclose them between
double quotes (<tt>"</tt>).
<p>
When writing both single characters and strings of characters in a constant way,
it is necessary to put the quotation marks to distinguish them from possible
variable identifiers or reserved words.  Notice this:
<blockquote><tt>
x<br>
'x'
</tt></blockquote>
<tt><B>x</B></tt> refers to variable <tt><B>x</B></tt>, whereas <tt><B>'x'</B></tt>
refers to the character constant <tt><B>'x'</B></tt>.
<p>
Character constants and string constants have certain peculiarities, like the
<b>escape codes</b>. These are special characters that cannot be expressed otherwise
in the sourcecode of a program, like <i>newline</i> (<tt>\n</tt>) or <i>tab</i>
(<tt>\t</tt>). All of them are preceded by an inverted slash (<tt>\</tt>). Here you have
a list of such escape codes:
<blockquote>
<table border="1">
<tr><td width="50" align="center"><tt><b>\n</b></tt></td><td width="250">newline</t></tr>
<tr><td width="50" align="center"><tt><b>\r</b></tt></td><td>carriage return</t></tr>
<tr><td width="50" align="center"><tt><b>\t</b></tt></td><td>tabulation</t></tr>
<tr><td width="50" align="center"><tt><b>\v</b></tt></td><td>vertical tabulation</t></tr>
<tr><td width="50" align="center"><tt><b>\b</b></tt></td><td>backspace</t></tr>
<tr><td width="50" align="center"><tt><b>\f</b></tt></td><td>page feed</t></tr>
<tr><td width="50" align="center"><tt><b>\a</b></tt></td><td>alert (beep)</t></tr>
<tr><td width="50" align="center"><tt><b>\'</b></tt></td><td>single quotes (<tt>'</tt>)</t></tr>
<tr><td width="50" align="center"><tt><b>\"</b></tt></td><td>double quotes (<tt>"</tt>)</t></tr>
<tr><td width="50" align="center"><tt><b>\?</b></tt></td><td>question (<tt>?</tt>)</t></tr>
<tr><td width="50" align="center"><tt><b>\\</b></tt></td><td>inverted slash (<tt>\</tt>)</t></tr>
</table>
</blockquote>
For example:
<blockquote><tt>
'\n'<br>'\t'<br>"Left \t Right"<br>"one\ntwo\nthree"
</tt></blockquote>
Additionally, you can express any character by its numerical ASCII code by
writing an inverted slash bar character (<tt>\</tt>) followed by the ASCII code expressed
as an octal (radix-8) or hexadecimal (radix-16) number. In the first case (octal) the number
must immediately follow the inverted slash (for example <tt><b>\23</b></tt> or
<tt><b>\40</b></tt>), in the second case (hexacedimal), you must put an <tt><b>x</b></tt>
character before the number (for example <tt><b>\x20</b></tt> or <tt><b>\x4A</b></tt>).<br>
[Consult the document <a href="../papers/ascii.html">ASCII Code</a> for more information
about this type of escape code].
<p>
coonstants of string of characters can be extended by more than a single code line
if each code line ends with an inverted slash (<tt>\</tt>):
<blockquote><tt>
"string expressed in \<br>
two lines"
</tt></blockquote>
You can also concatenate several string constants separating them by one or several
blankspaces, tabulators, newline or any other valid blank character:
<blockquote><tt><pre>
"we form" "a single" "string" "of characters"
</pre></tt></blockquote>

<p>
<h2>Defined constants (<i><tt>#define</tt></i>)</h2>
You can define your own names for constants that you use quite often without having
to resort to variables, simply by using the <TT><B>#define</B></TT>
preprocessor directive. This is its format:
<blockquote><tt>
<b>#define </b><i>identifier value</i>
</tt></blockquote>
For example:
<blockquote><tt>
#define PI 3.14159265<br>
#define NEWLINE '\n'<br>
#define WIDTH 100<br>
</tt></blockquote>
they define three new constants. Once they are declared,
you are able to use them in the rest of the code as
any if they were any other constant, for example:
<blockquote><tt>
circle = 2 * PI * r;<br>
cout &lt;&lt; NEWLINE;
</tt></blockquote>
In fact the only thing that the compiler does when it finds <tt><B>#define</B></tt>
directives is to replace literally any occurrence of the them (in the previous example,
<tt><B>PI</B></tt>, <tt><B>NEWLINE</B></tt> or <tt><B>WIDTH</B></tt>)
by the code to which they have been defined (<tt><B>3.14159265</B></tt>,
<tt><B>'\n'</B></tt> and <tt><B>100</B></tt>, respectively).  For this reason,
<tt>#define</tt> constants are considered <i>macro constants</i>.
<p>
The <tt>#define</tt> directive is not a code instruction, it is a directive
for the preprocessor, therefore it assumes the whole line as the directive and
does not require a semicolon (<tt>;</tt>) at the end of it.
If you include a semicolon character (<tt>;</tt>) at the end, it will also be added
when the preprocessor will substitute any occurence of the defined constant
within the body of the program.
<h2>declared constants (<tt>const</tt>)</h2>
With the <tt><b>const</b></tt> prefix you can declare constants with a specific type
exactly as you would do with a variable:
<blockquote><tt>
const int width = 100;<br>
const char tab = '\t';<br>
const zip = 12440;<br>
</tt></blockquote>
In case that the type was not specified (as in the last example) the compiler assumes
that it is type <TT><B>int</B></TT>.

<!--cuatut-->
<P>
<CENTER><TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=0 BORDER=0>
 <TR><TD BGCOLOR="#0000FF"><IMG SRC="head0.gif" WIDTH=2 HEIGHT=2></TD></TR>
 <TR><TD ALIGN="right"><FONT FACE="arial,helvetica" SIZE=1>&copy; The C++ Resources Network, 2000-2003 - All rights reserved</FONT></TD></TR>
</TABLE></CENTER>
<P>
<CENTER>
<TABLE CELLPADDING=0 WIDTH=100%>
<TR><TD ALIGN="right" WIDTH=45%><A HREF="tut1-1.html">
 <IMG SRC="butnback.gif" ALIGN="right" BORDER=0>
 Previous:<BR><B>1-1. Structure of a C++ program.</B></A></TD>
<TD ALIGN="center" WIDTH=10%><A HREF="index.html">
 <IMG SRC="butnindx.gif" BORDER=0><BR>
 index</A></TD>
<TD ALIGN="left" WIDTH=45%><A HREF="tut1-3.html">
 <IMG SRC="butnnext.gif" ALIGN="left" BORDER=0>
 Next:<BR><B>1-3. Operators.</B></A>
</TD></TR></TABLE>
</CENTER>
<!--/cuatut-->

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -