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

📄 教学--第五章 变量和常量.htm

📁 《白话c++网页版》是一本用浅显易懂的并具有点幽默的语调来讲述c++的高深的内容
💻 HTM
📖 第 1 页 / 共 5 页
字号:
      <P>......</P>
      <P>int main(int argc, char* argv[])]</P>
      <P>{</P>
      <P>&nbsp;/////////////////定义变量//////////////////////////////////////////////////<BR>&nbsp;&nbsp;&nbsp; 
      //以下定义三个变量:a,b,c</P>
      <P>&nbsp; int a;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</P>
      <P>&nbsp; double b,c;&nbsp;</P>
      <P> </P>
      <P>&nbsp; //用 cout 输出三个变量:</P>
      <P>&nbsp; cout &lt;&lt; "a = " &lt;&lt; a &lt;&lt; " b = " &lt;&lt; b 
      &lt;&lt; " c = " &lt;&lt; c &lt;&lt; endl;</P>
      <P> </P>
      <P>/////////////////初始化变量////////////////////////////////////////////////<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
      int d = 0;</P>
      <P>&nbsp; float e =&nbsp; 1.234, f;</P>
      <P>&nbsp; f = 567.8 + 0.2;</P>
      <P>&nbsp; cout &lt;&lt; "d = " &lt;&lt; d &lt;&lt; " e = " &lt;&lt; e 
      &lt;&lt; " f = " &lt;&lt; f &lt;&lt; endl;</P>
      <P><BR>&nbsp;&nbsp; 
      /////////////////变量值越界//////////////////////////////////////////////////<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
      int g = 3000000000; //给g赋值超过int允许的范围,所以g的值不可能如愿等于3000000000</P>
      <P>&nbsp; cout &lt;&lt; "g = " &lt;&lt; g &lt;&lt;endl;</P>
      <P> </P>
      <P><B>/////////////////bool类型的"真面目"//////////////////////////////////////////<BR>&nbsp;&nbsp;&nbsp; 
      bool h = false ,i = true;&nbsp;&nbsp;</B></P>
      <P><B>&nbsp; cout &lt;&lt; "h = " &lt;&lt; h &lt;&lt; " i = " &lt;&lt; i 
      &lt;&lt; endl;&nbsp;</B></P>
      <P></P>
      <P>&nbsp; getchar();</P>
      <P>&nbsp; return 0;</P>
      <P>}</P>
      <P>运行后……原来,false是0,true是1。</P>
      <P> </P>
      <P><B>3). char 类型的特殊</B></P>
      <P> </P>
      <P>char 的范围是 -128 ~ 127</P>
      <P>unsigned char 的范围是 0 ~ 255</P>
      <P>那么按照前面的说法,我们可以为这样为一个字符类型的变量赋值:</P>
      <P> </P>
      <P>char c = 120;</P>
      <P>unsigned char uc = 250;</P>
      <P> </P>
      <P>这样看来,所谓的“字符”类型,似乎除了取值范围小一点以外,和整型也没有什么区别。这句话的确没错。对于C、C++来说,字符类型完全可以当成一个整数来对待。</P>
      <P>事实上,所有信息在计算机里,都是使用数字来表达。英文字母 'A' 在计算机里表示为 65; 字母 'B' 
      表示为66。所有你在键盘可以看到的字符,如大小写英文字母,阿拉伯数字符号,标点符号都可以有一个相应的数值表示。</P>
      <P>但要让我们记住65就是'A',而33就 
'!'等255个对应关系,显然很折磨人,所以,计算机高级语言允许我们直接为字符类型变量这样赋值:</P>
      <P>char&nbsp; c = 'A';&nbsp;&nbsp;</P>
      <P>char&nbsp; d = '!'; //英文感叹号</P>
      <P>char&nbsp; e = '.'; //英文句号</P>
      <P>char&nbsp; f = ' '; //空格</P>
      <P>即:将所要得到的字符用单引号括住。(引号''是英文状态下的,千万不要使用中文符号)</P>
      <P> </P>
      <P>另外,对于一个数值类型,如果它等于120,那么输出时显示的120,如果是一个字符类型,输出却是120对应的字符。也就是说:</P>
      <P>int&nbsp; k = 120;</P>
      <P>char j = 120;</P>
      <P>二者虽然值都为120,但输出j时,计算机并不显示120这个值,而是120对应的字符。试试看!</P>
      <P> </P>
      <P>为了不让教程中的代码重复占用版面,省略号省略掉的代码要多点了……</P>
      <P> </P>
      <P>......</P>
      <P> </P>
      <P><B>/////////////////char 
      类型///////////////////////////////////////////////////</B></P>
      <P><B>int k = 120;</B></P>
      <P><B>char j = 120;</B></P>
      <P><B>cout &lt;&lt; "k(int) = " &lt;&lt; k &lt;&lt; " j(char) = " &lt;&lt; 
      j &lt;&lt; endl;</B></P>
      <P> </P>
      <P>getchar();</P>
      <P>......<BR></P>
      <P>输出结果,k当然是120,但j,原来120对应的字母是 
      'x'。写的是120,输出的却是x,很不直观对不?所以,除非我们故意要和自已或者其他看代码的人玩“密码”,否则,还是直接想要什么字符,就写哪个字符吧。</P>
      <P> </P>
      <P>/////////////////char 
      类型///////////////////////////////////////////////////</P>
      <P>int k = 120;</P>
      <P>char j = 120;</P>
      <P>cout &lt;&lt; "k(int) = " &lt;&lt; k &lt;&lt; " j(char) = " &lt;&lt; j 
      &lt;&lt; endl;</P>
      <P> </P>
      <P>char l = 'A';</P>
      <P>char m = l + 1;</P>
      <P>cout &lt;&lt; "l = " &lt;&lt; l &lt;&lt; " m = " &lt;&lt; m &lt;&lt; 
      endl;</P>
      <P> </P>
      <P>getchar();</P>
      <P>......<BR></P>
      <P>输出结果,l为 'A',而 m 为 'B',想一想,为什么?学完后面内容就有答案。</P>
      <P> </P>
      <P>单引号本身也是一个字符,如何表示单引号呢?是否用 ''' 来表示?看明白下面的常用字符ASCII码表以后再说。</P>
      <P> </P>
      <P>(ASCII是指:American Standard Code for Information 
      Interchange,美国信息交换标准码。)</P>
      <TABLE height=352 width="100%" border=1>
        <TBODY>
        <TR>
          <TD width="12%" bgColor=#c0c0c0 height=19>值</TD>
          <TD width="12%" height=19>符号</TD>
          <TD width="12%" bgColor=#c0c0c0 height=19>值</TD>
          <TD width="12%" height=19>符号</TD>
          <TD width="13%" bgColor=#c0c0c0 height=19>值</TD>
          <TD width="13%" height=19>符号</TD></TR>
        <TR>
          <TD width="12%" bgColor=#c0c0c0 height=19>0</TD>
          <TD width="12%" height=19>空字符</TD>
          <TD width="12%" bgColor=#c0c0c0 height=19>44</TD>
          <TD width="12%" height=19>,</TD>
          <TD width="13%" bgColor=#c0c0c0 height=21>91</TD>
          <TD width="13%" height=19>[</TD></TR>
        <TR>
          <TD width="12%" bgColor=#c0c0c0 height=19>32</TD>
          <TD width="12%" height=19>空格</TD>
          <TD width="12%" bgColor=#c0c0c0 height=19>45</TD>
          <TD width="12%" height=19>-</TD>
          <TD width="13%" bgColor=#c0c0c0 height=19>92</TD>
          <TD width="13%" height=19>\</TD></TR>
        <TR>
          <TD width="12%" bgColor=#c0c0c0 height=19>33</TD>
          <TD width="12%" height=19>!</TD>
          <TD width="12%" bgColor=#c0c0c0 height=19>46</TD>
          <TD width="12%" height=19>.</TD>
          <TD width="13%" bgColor=#c0c0c0 height=19>93</TD>
          <TD width="13%" height=19>]</TD></TR>
        <TR>
          <TD width="12%" bgColor=#c0c0c0 height=19>34</TD>
          <TD width="12%" height=19>"</TD>
          <TD width="12%" bgColor=#c0c0c0 height=19>47</TD>
          <TD width="12%" height=19>/</TD>
          <TD width="13%" bgColor=#c0c0c0 height=19>94</TD>
          <TD width="13%" height=19>^</TD></TR>
        <TR>
          <TD width="12%" bgColor=#c0c0c0 height=19>35</TD>
          <TD width="12%" height=19>#</TD>
          <TD width="12%" bgColor=#c0c0c0 height=19>48 ~ 57</TD>
          <TD width="12%" height=19>0 ~ 9&nbsp;</TD>
          <TD width="13%" bgColor=#c0c0c0 height=19>95</TD>
          <TD width="13%" height=19>-</TD></TR>
        <TR>
          <TD width="12%" bgColor=#c0c0c0 height=19>36</TD>
          <TD width="12%" height=19>$</TD>
          <TD width="13%" bgColor=#c0c0c0 height=19>58</TD>
          <TD width="12%" height=19>:</TD>
          <TD width="13%" bgColor=#c0c0c0 height=19>96</TD>
          <TD width="13%" height=19>`</TD></TR>
        <TR>
          <TD width="12%" bgColor=#c0c0c0 height=19>37</TD>
          <TD width="12%" height=19>%</TD>
          <TD width="13%" bgColor=#c0c0c0 height=19>59</TD>
          <TD width="12%" height=19>;</TD>
          <TD width="13%" bgColor=#c0c0c0 height=19>97 ~ 122</TD>
          <TD width="13%" height=19>a ~ z</TD></TR>
        <TR>
          <TD width="12%" bgColor=#c0c0c0 height=19>38</TD>
          <TD width="12%" height=19>&amp;</TD>
          <TD width="13%" bgColor=#c0c0c0 height=19>60</TD>
          <TD width="12%" height=19>&lt;</TD>
          <TD width="13%" bgColor=#c0c0c0 height=19>123</TD>
          <TD width="13%" height=19>{</TD></TR>
        <TR>
          <TD width="12%" bgColor=#c0c0c0 height=19>39</TD>
          <TD width="12%" height=19>'</TD>
          <TD width="13%" bgColor=#c0c0c0 height=19>61</TD>
          <TD width="12%" height=19>=</TD>
          <TD width="13%" bgColor=#c0c0c0 height=19>124</TD>
          <TD width="13%" height=19>|</TD></TR>
        <TR>
          <TD width="12%" bgColor=#c0c0c0 height=21>40</TD>
          <TD width="12%" height=21>(</TD>
          <TD width="13%" bgColor=#c0c0c0 height=21>62</TD>
          <TD width="12%" height=21>&gt;</TD>
          <TD width="13%" bgColor=#c0c0c0 height=19>125</TD>
          <TD width="13%" height=21>}</TD></TR>
        <TR>
          <TD width="12%" bgColor=#c0c0c0 height=19>41</TD>
          <TD width="12%" height=19>)</TD>
          <TD width="13%" bgColor=#c0c0c0 height=19>63</TD>
          <TD width="12%" height=19>?</TD>
          <TD width="13%" bgColor=#c0c0c0 height=19>126</TD>
          <TD width="13%" height=19>~</TD></TR>
        <TR>
          <TD width="12%" bgColor=#c0c0c0 height=19>42</TD>
          <TD width="12%" height=19>*</TD>
          <TD width="13%" bgColor=#c0c0c0 height=19>64</TD>
          <TD width="12%" height=19>@</TD>
          <TD width="13%" bgColor=#c0c0c0 height=19>127</TD>
          <TD width="13%" height=19>DEL (Delete键)</TD></TR>
        <TR>
          <TD width="12%" bgColor=#c0c0c0 height=19>43</TD>
          <TD width="12%" height=19>+</TD>
          <TD width="13%" bgColor=#c0c0c0 height=19>65 - 90</TD>
          <TD width="12%" height=19>A ~ Z</TD>
          <TD width="13%" bgColor=#c0c0c0 height=19>&nbsp;</TD>
          <TD width="13%" height=19>&nbsp;</TD></TR></TBODY></TABLE>
      <P>(其中,0~31都是一些不可见的字符,所以这里只列出值为0的字符,值为0的字符称为空字符,输出该字符时,计算机不会有任何反应。我们以后会学习0字符的特殊作用。)</P>
      <P> </P>
      <P><B>4). 转义符的使用</B></P>
      <P> </P>
      <P>根据前面的说法,单引号应该表达为:</P>
      <P>char c = ''';&nbsp;</P>
      <P>但这是错误的。C、C++不认识 ''',因为它容易引起歧义。</P>

⌨️ 快捷键说明

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