2014.12.26 整型,字符型,布尔型,浮点型

本文详细介绍了C++中的各种数据类型,包括整型、字符类型、宽字符类型、布尔类型和浮点类型。深入探讨了每种类型的表示范围、如何获取类型尺寸、整型溢出的概念、以及使用科学计数法表示大数值的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<p>最小的存储单元是byte(字节),但是在C++中并没有这样的类型,只有char</p> <h4>有符号整型</h4> <ul> <ul> <li>short&#160; <ul> <li>16位 能表示范围&#160; -2^15 到 2^15 – 1&#160;&#160; = –32768 到 32767</li> </ul> </li> </ul> </ul> <p>&#160;</p> <ul> <ul> <li>int&#160;&#160;&#160;&#160;&#160; <ul> <li>32位&#160; -2^31&#160; 到 -2^31 – 1</li> </ul> </li> </ul> </ul> <p>&#160;</p> <ul> <ul> <li>long <ul> <li>可变的,如果在32位系统下 long = 32位 ,如果在64位系统下 long = 64</li> </ul> </li> </ul> </ul> <p>&#160;</p> <ul> <ul> <li>long long <ul> <li>永远64位&#160; -2^63&#160; 到 -2^63 – 1&#160;&#160;&#160;&#160; </li> </ul> </li> </ul> </ul> <h4>无符号整型 (不能表示负数了)</h4> <ul> <ul> <li>unsigned short&#160; 0 - 2^16 – 1&#160;&#160; = 0 到 65535 </li> </ul> </ul> <h4>&#160;</h4> <h4>获取数据类型尺寸</h4> <blockquote> <p>获取数据类型尺寸的结果是字节数</p> <pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"><span style="color: #008000">//结果为字节 byte</span> </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> cout &lt;&lt; &quot;<span style="color: #8b0000">size of (short) = </span>&quot; &lt;&lt; <span style="color: #0000ff">sizeof</span>(<span style="color: #0000ff">short</span>) &lt;&lt;&quot;<span style="color: #8b0000"> byte</span>&quot; &lt;&lt; endl; </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> cout &lt;&lt; &quot;<span style="color: #8b0000">size of (int) = </span>&quot; &lt;&lt; <span style="color: #0000ff">sizeof</span>(<span style="color: #0000ff">int</span>) &lt;&lt; &quot;<span style="color: #8b0000"> byte</span>&quot; &lt;&lt; endl; </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> cout &lt;&lt; &quot;<span style="color: #8b0000">size of (long) = </span>&quot; &lt;&lt; <span style="color: #0000ff">sizeof</span>(<span style="color: #0000ff">long</span>) &lt;&lt; &quot;<span style="color: #8b0000"> byte</span>&quot; &lt;&lt; endl; </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> cout &lt;&lt; &quot;<span style="color: #8b0000">size of (long long) = </span>&quot; &lt;&lt; <span style="color: #0000ff">sizeof</span>(_Longlong) &lt;&lt; &quot;<span style="color: #8b0000"> byte</span>&quot; &lt;&lt; endl; </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> <span style="color: #008000">//还可以输出最大值和最小值 不用sizeof方法</span> </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> cout &lt;&lt; LONG_MAX &lt;&lt; endl; </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> cout &lt;&lt; ULLONG_MAX &lt;&lt; endl;</pre></pre> </blockquote>

<p>&#160;</p>

<h4>整型溢出</h4>

<blockquote> <p>大多数语言是允许整型溢出的</p> </blockquote>

<p>&#160;</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,&#39;Courier New&#39;,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,&#39;Courier New&#39;,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,&#39;Courier New&#39;,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,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff">wcout &lt;&lt; &quot;<span style="color: #8b0000">c1 = </span>&quot;&lt;&lt;c1&lt;&lt;endl; <span style="color: #008000">//输出是字符 这边不一定输出的是&quot;中&quot;字,因为wcout指的是宽字节不一定是unicode</span> </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> wcout &lt;&lt; L&quot;<span style="color: #8b0000">中</span>&quot; &lt;&lt; endl;<span style="color: #008000">//这里标明了L 一定是输出&quot;中&quot;字 </span>

</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,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,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,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,&#39;Courier New&#39;,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,&#39;Courier New&#39;,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>&#160;</p></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> flag1 = 100;<span style="color: #008000">//布尔类型可以赋值整型 非0的为真 0为假</span></pre></pre> </blockquote>

<pre>&#160;</pre>

<h4>浮点类型</h4>

<ul> <ul> <li>float 单精度 占用4byte</li> </ul> </ul>

<p>&#160;</p>

<ul> <ul> <li>double 双精度 占用8byte</li> </ul> </ul>

<p>&#160;</p>

<ul> <ul> <li>long double 长双精度占用16byte</li> </ul> </ul>

<blockquote> <h5></h5> </blockquote>

<h5>科学计数法 <pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,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,&#39;Courier New&#39;,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,&#39;Courier New&#39;,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,&#39;Courier New&#39;,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,&#39;Courier New&#39;,courier,monospace; margin: 0em; width: 100%; background-color: #ffffff"> <span style="color: #0000ff">double</span> val3 = 1.23E7;</pre></pre>

<p>&#160; float最大能表示指数为&#160; +38 – 37&#160;&#160; </p>

<p>double +308 –307 </p></h5>

转载于:https://my.oschina.net/u/2297730/blog/361018

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值