<p>最小的存储单元是byte(字节),但是在C++中并没有这样的类型,只有char</p> <h4>有符号整型</h4> <ul> <ul> <li>short  <ul> <li>16位 能表示范围  -2^15 到 2^15 – 1   = –32768 到 32767</li> </ul> </li> </ul> </ul> <p> </p> <ul> <ul> <li>int      <ul> <li>32位  -2^31  到 -2^31 – 1</li> </ul> </li> </ul> </ul> <p> </p> <ul> <ul> <li>long <ul> <li>可变的,如果在32位系统下 long = 32位 ,如果在64位系统下 long = 64</li> </ul> </li> </ul> </ul> <p> </p> <ul> <ul> <li>long long <ul> <li>永远64位  -2^63  到 -2^63 – 1     </li> </ul> </li> </ul> </ul> <h4>无符号整型 (不能表示负数了)</h4> <ul> <ul> <li>unsigned short  0 - 2^16 – 1   = 0 到 65535 </li> </ul> </ul> <h4> </h4> <h4>获取数据类型尺寸</h4> <blockquote> <p>获取数据类型尺寸的结果是字节数</p> <pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"><span style="color: #008000">//结果为字节 byte</span> </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> cout << "<span style="color: #8b0000">size of (short) = </span>" << <span style="color: #0000ff">sizeof</span>(<span style="color: #0000ff">short</span>) <<"<span style="color: #8b0000"> byte</span>" << endl; </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> cout << "<span style="color: #8b0000">size of (int) = </span>" << <span style="color: #0000ff">sizeof</span>(<span style="color: #0000ff">int</span>) << "<span style="color: #8b0000"> byte</span>" << endl; </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> cout << "<span style="color: #8b0000">size of (long) = </span>" << <span style="color: #0000ff">sizeof</span>(<span style="color: #0000ff">long</span>) << "<span style="color: #8b0000"> byte</span>" << endl; </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> cout << "<span style="color: #8b0000">size of (long long) = </span>" << <span style="color: #0000ff">sizeof</span>(_Longlong) << "<span style="color: #8b0000"> byte</span>" << endl; </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> <span style="color: #008000">//还可以输出最大值和最小值 不用sizeof方法</span> </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> cout << LONG_MAX << endl; </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> cout << ULLONG_MAX << endl;</pre></pre> </blockquote>
<p> </p>
<h4>整型溢出</h4>
<blockquote> <p>大多数语言是允许整型溢出的</p> </blockquote>
<p> </p>
<h4>字符类型</h4>
<blockquote> <p>c++里面是没有byte类型的,一个字符类型兼两个作用</p> </blockquote>
<ul> <ul> <li>存储标准ascll码 0到127共128个字符 </li>
<li>当作有符号的字节类型</li>
</ul> </ul>
<h4>宽字符类型</h4>
<ul> <ul> <li>宽字符类型 wchar_t 以32位来存储字符 </li>
<li>c++11里面新出的两个宽字符类型 char16_t (16位) char32_t(32位)</li>
</ul> </ul>
<blockquote> <pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff">wchar_t c1 = L'中'; <span style="color: #008000">//前面加L</span> </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> <span style="color: #0000ff">char</span>16_t c2 = L'中'; </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> <span style="color: #0000ff">char</span>32_t c3 = L'中';</pre></pre>
<h5>宽字节的输入输出</h5>
<pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff">wcout << "<span style="color: #8b0000">c1 = </span>"<<c1<<endl; <span style="color: #008000">//输出是字符 这边不一定输出的是"中"字,因为wcout指的是宽字节不一定是unicode</span> </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> wcout << L"<span style="color: #8b0000">中</span>" << endl;<span style="color: #008000">//这里标明了L 一定是输出"中"字 </span>
</pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> cout << "<span style="color: #8b0000">c1的编码是 : </span>" << c1 << endl;<span style="color: #008000">//输出的是十进制编码是20013 转成十六进制是4E2D</span> </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> cout << "<span style="color: #8b0000">\u4e2d</span>" << endl;<span style="color: #008000">//通过unicode编码输出字符 这边输出的肯定是"中"字 \u代表了unicode</span></pre></pre>
</blockquote>
<h4>布尔类型</h4>
<blockquote> <pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"><span style="color: #0000ff">bool</span> flag1 = <span style="color: #0000ff">true</span>; </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"><p> <span style="color: #0000ff">bool</span> flag2 = <span style="color: #0000ff">false</span>;</p> <p> </p></pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> flag1 = 100;<span style="color: #008000">//布尔类型可以赋值整型 非0的为真 0为假</span></pre></pre> </blockquote>
<pre> </pre>
<h4>浮点类型</h4>
<ul> <ul> <li>float 单精度 占用4byte</li> </ul> </ul>
<p> </p>
<ul> <ul> <li>double 双精度 占用8byte</li> </ul> </ul>
<p> </p>
<ul> <ul> <li>long double 长双精度占用16byte</li> </ul> </ul>
<blockquote> <h5></h5> </blockquote>
<h5>科学计数法 <pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"><span style="color: #0000ff">float</span> val1 = 12.34; </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> <span style="color: #0000ff">double</span> val2 = 12.56; </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> <span style="color: #008000">//如果数字比较大的时候可以用科学计数法 比如12300000后面有5个0那么多</span> </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> <span style="color: #008000">//科学计数法 1尾数 2e/E 3指数</span> </pre><pre style="font-size: 12px; font-family: consolas,'Courier New',courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> <span style="color: #0000ff">double</span> val3 = 1.23E7;</pre></pre>
<p>  float最大能表示指数为  +38 – 37   </p>
<p>double +308 –307 </p></h5>